利用python的scrapy框架中的css选择器获取不到元素内容
高洛峰
高洛峰 2017-04-18 10:12:41
0
2
726

利用python中的scrapy框架的css选择器对具体标签内容进行获取,但是获取不到内容。
当前网页源码(是js渲染之前的代码):

css选择器代码:urllist = response.css('ul.nav li a::attr(href)')[0::3].extract()
运行结果是:
urllist===================[]
urllist长度============ 0
css选择器内的代码应该是没有错误的,为什么获取不到内容?
由于怀疑是css选择器出现了问题,因此替换xpath选择器,
xpath选择器代码:urllist=response.xpath('//ul[@class ="nav"]/li/a/@href').extract()
但是运行结果和css选择器相同。内容仍为空,长度为0

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
洪涛

Maybe your problem is not with the css selector code. Check whether the response content is consistent with what you see on the webpage

左手右手慢动作

You try to use scrapy shell in the command line to add the target url and then you can get a response object. First check whether the response object is normal. You can first check the response.body to see if it is what you want to crawl. The source code of the web page
and then use this response object to debug the code of your css selector

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!