scrapy边爬取边对字符进行分割

scrapy边爬虫边对爬取到的文本进行分割

爬取的目标网页如下:

766020adf30099eba3a3bfa73bcec576dac.jpg

拿到a标签的text之后,还需要分别取到公司名称和公司id,这个时候就需要对字符串进行split了。这时直接对爬取道的内容进行split然后往item里append内容会出现keyerror错误,而改成下面的代码就成功了(这里借用了第三方list,然后对第三方list进行操作,再赋值)。

错误代码(keyerror):

LeastRepComList = response.xpath(
    "//table[@class='da_tbl'][3]/tr/td[1]/div/p/a/text()").extract()
for LeastRepCom in LeastRepComList:
    item['LeastRepComID'].append(LeastRepCom.split()[1].strip())
    item['LeastRepComName'].append(LeastRepCom.split()[0].strip())

正确代码:

LeastRepComList = response.xpath(
    "//table[@class='da_tbl'][3]/tr/td[1]/div/p/a/text()").extract()
for LeastRepCom in LeastRepComList:
    LeastRepComIdList.append(LeastRepCom.split()[1].strip())
    LeastRepComNameList.append(LeastRepCom.split()[0].strip())
item['LeastRepComID'] = LeastRepComIDList
item['LeastRepComName'] = LeastRepComNameList

虽然问题解决了,但是还是不清楚原理,初步猜想跟scrapy框架的异步多线程有关??,本人入门党一枚,期待大神解答!!!!

 

转载于:https://my.oschina.net/u/3636678/blog/1859657

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值