Scrapy抓取数据循环报AttributeError: 'str' object has no attribute 'css'
来源:互联网 发布:三星s4网络锁解锁软件 编辑:程序博客网 时间:2024/06/13 23:31
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
打印正常!
昨天安装了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 scrapyfrom scrapy.selector import Selectorfrom scrapy.http import HtmlResponseclass 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))
阅读全文
0 0
- Scrapy抓取数据循环报AttributeError: 'str' object has no attribute 'css'
- Django 错误信息:AttributeError: 'str' object has no attribute '_meta'
- Django: AttributeError: 'str' object has no attribute 'resolve'
- Scrapy安装报错:AttributeError: 'module' object has no attribute 'OP_NO_TLSv1_1'解决办法
- scrapy出现:AttributeError: 'list' object has no attribute 'xpath'
- Scrapy AttributeError: 'module' object has no attribute 'OP_NO_TLSv1_1 解决方法
- python3 报错: AttributeError: 'module' object has no attribute 'urlopen'
- 报错AttributeError: 'module' object has no attribute 'HTTPSConnection'
- 报错 AttributeError: 'module' object has no attribute 'bool_'
- Tensorflow报错:AttributeError: 'module' object has no attribute 'summary'
- 【Tensorflow】报错:AttributeError: 'module' object has no attribute 'scalar_summary'
- import module报错AttributeError: 'module' object has no attribute
- Tensorflow报错:AttributeError: 'module' object has no attribute 'scalar_summary'
- pandas报错AttributeError: 'module' object has no attribute 'plotting'
- tensorflow报错:AttributeError: 'module' object has no attribute 'get_or_create_global_step'
- AttributeError: 'classmethod' object has no attribute '__module__'
- AttributeError: 'module' object has no attribute 'getpass'
- AttributeError: ‘NoneType’ object has no attribute ‘clone’
- 云星数据---Apache Flink实战系列(精品版)】:Flink流处理API详解与编程实战002-Flink基于流的wordcount示例002
- Docker入门
- RabbitMQ消息队列基础概念详细讲解
- 记录下os.path.dirname(__file__)使用
- IDEA使用的一些常见操作
- Scrapy抓取数据循环报AttributeError: 'str' object has no attribute 'css'
- 生成datatable
- Android学习——systemUIService 之PowerUI分析
- 深度解析continue,break和return
- 16进制小技巧
- 先码后看 SpringMVC入门全介绍 侵立删
- H264学习笔记(1):视频压缩编码的基本原理
- JDK1.9环境变量配置
- 局域网内Linux服务器时间同步