Scrapy抓取数据循环报AttributeError: 'str' object has no attribute 'css'
2017-11-20 14:55
981 查看
AttributeError: 'str' object has no attribute 'css'
昨天安装了Scrapy之后,按捺不住骚动的心情,想尝试抓取数据
按照网上给的一个例子,从mm.taobao.com上抓取数据。
因为本地安装的Python版本是3.6,而网上给出的是2.7。只能自己想办法了
按照正常的流程抓取网页print之后可以正常打印。
但是在进行循环的时候,提示AttributeError: 'str' object has no attribute 'css'。
注意:python脚本是从上到下的顺序方式执行的,下方的报错,并不影响前面正常的执行,执行到当前段失败的时候再报错。
前面在执行response.css()的时候,是正常的,在进行循环的时候。
提示:AttributeError提示:AttributeError: 'str' object has no attribute 'css'
错误信息应该是当前执行在循环中的不是一个对象。而是一个Str。
找到错误点,那解决就比较简单了,直接将其进行强制转换为Selector
Selector(text=person).css()
再执行scrapy crawl taobao
打印正常!
提示:在使用Selector的时候,记得from 引用Selector
代码:
昨天安装了Scrapy之后,按捺不住骚动的心情,想尝试抓取数据
按照网上给的一个例子,从mm.taobao.com上抓取数据。
因为本地安装的Python版本是3.6,而网上给出的是2.7。只能自己想办法了
按照正常的流程抓取网页print之后可以正常打印。
但是在进行循环的时候,提示AttributeError: 'str' object has no attribute 'css'。
注意:python脚本是从上到下的顺序方式执行的,下方的报错,并不影响前面正常的执行,执行到当前段失败的时候再报错。
前面在执行response.css()的时候,是正常的,在进行循环的时候。
提示:AttributeError提示:AttributeError: 'str' object has no attribute 'css'
错误信息应该是当前执行在循环中的不是一个对象。而是一个Str。
找到错误点,那解决就比较简单了,直接将其进行强制转换为Selector
Selector(text=person).css()
再执行scrapy crawl taobao
打印正常!
提示:在使用Selector的时候,记得from 引用Selector
代码:
import scrapy from scrapy.selector import Selector from scrapy.http import HtmlResponse class TaobaoSpider(scrapy.Spider): name="taobao" #标识爬虫 def start_requests(self): print("-------------------- scrapy begin---------------\n") urls=[ 'https://mm.taobao.com/json/request_top_list.htm?page=1', ] for url in urls: yield scrapy.Request(url=url,callback=self.parse) #下载后的回调方法 def parse(self, response): page=response.url.split("?")[-1] persons =response.css(".list-item").extract() for person in response.css(".list-item").extract(): person=Selector(text=person) person_name=person.css(".lady-name::text").extract() print("\nname:%s"%person_name) person_name = person.css(".lady-name::text").extract()[0] person_img = person.css(".pic a img::attr(src)").extract()[0] person_age = person.css("em strong::text").extract()[0] person_address = person.css("span::text").extract()[0] person_profession = person.css("em::text").extract()[1] person_fans = person.css("em strong ::text").extract()[1] person_url = person.css(".lady-name::attr(href)").extract()[0] person. print("personal-info\n姓名:%s\n图片:%s\n年龄:%s\n职业:%s\n地址:%s\n粉丝:%s\n主页:%s" %(person_name,person_img,person_age,person_profession,person_address,person_fans,person_url))
相关文章推荐
- Django 错误信息:AttributeError: 'str' object has no attribute '_meta'
- python 提示错误AttributeError: type object 'str' has no attribute '_name_'
- Django 错误信息:AttributeError: 'str' object has no attribute '_meta'
- Scrapy安装报错:AttributeError: 'module' object has no attribute 'OP_NO_TLSv1_1'解决办法
- python3.6报错:AttributeError: 'str' object has no attribute 'decode'
- python机器学习实战3.2提示错误AttributeError: type object 'str' has no attribute '_name_'
- Scrapy报错Error: 'module' object has no attribute 'OP_SINGLE_ECDH_USE'
- caffe输入层问题AttributeError: 'RoIDataLayer' object has no attribute 'param_str_'
- python遇到AttributeError: 'str' object has no attribute '_getAssertEqualityFunc'问题解决过程
- scrapy出现:AttributeError: 'list' object has no attribute 'xpath'
- scrapy:使用response.follow()方法时出现AttributeError: 'HtmlResponse' object has no attribute 'follow'
- Django: AttributeError: 'str' object has no attribute 'resolve'
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- django view.py error 'function' object has no attribute 'objects' 解决办法
- Flask--AttributeError 'bool' object has no attribute '__call__'
- pyspark ArrtibuteError: 'builtin_function_or_method' object has no attribute '__code__'
- python图像处理问题AttributeError: 'NoneType' object has no attribute 'shape'
- AttributeError: 'Response' object has no attribute 'json'