Python 如何调试Scrapy?
我99%确定我的Python 如何调试Scrapy?,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,我99%确定我的hxs有问题。请选择此网站上的。我提取不出任何东西。当我运行下面的代码时,我没有得到任何错误反馈标题或链接未填充。有什么帮助吗 def parse(self, response): self.log("\n\n\n We got data! \n\n\n") hxs = HtmlXPathSelector(response) sites = hxs.select('//div[@class=\'footer\']') items
hxs有问题。请选择此网站上的。我提取不出任何东西。当我运行下面的代码时,我没有得到任何错误反馈<代码>标题
或链接
未填充。有什么帮助吗
def parse(self, response):
self.log("\n\n\n We got data! \n\n\n")
hxs = HtmlXPathSelector(response)
sites = hxs.select('//div[@class=\'footer\']')
items = []
for site in sites:
item = CarrierItem()
item['title'] = site.select('.//a/text()').extract()
item['link'] = site.select('.//a/@href').extract()
items.append(item)
return items
有什么方法可以调试这个吗?我还尝试将scrapy shell
命令与url一起使用,但当我在shell中输入view(response)
时,它只返回True
,并打开一个文本文件而不是我的Web浏览器
>>> response.url
'https://qvpweb01.ciq.labs.att.com:8080/dis/login.jsp'
>>> hxs.select('//div')
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'NoneType' object has no attribute 'select'
>>> view(response)
True
>>> hxs.select('//body')
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'NoneType' object has no attribute 'select'
>>>response.url
'https://qvpweb01.ciq.labs.att.com:8080/dis/login.jsp'
>>>选择('//div')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“非类型”对象没有属性“选择”
>>>视图(响应)
真的
>>>选择(“//正文”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“非类型”对象没有属性“选择”
Scrapy shell确实是一个很好的工具。如果您的文档有一个XML样式表,那么它可能是一个XML文档。因此,您可以将scrapy shell与xxs
一起使用,而不是使用hxs
,如本scrapy文档中关于删除名称空间的示例所示:
当这不起作用时,我倾向于返回纯lxml.etree并转储整个文档的元素:
import lxml.etree
import lxml.html
class myspider(BaseSpider):
...
def parse(self, response):
self.log("\n\n\n We got data! \n\n\n")
root = lxml.etree.fromstring(response.body).getroot()
# or for broken XML docs:
# root = lxml.etree.fromstring(response.body, parser = lxml.etree.XMLParser(recover=True)).getroot()
# or for HTML:
# root = lxml.etree.fromstring(response.body, parser=lxml.html.HTMLParser()).getroot()
# and then lookup what are the actual elements I can select
print list(root.iter()) # this could be very big, but at least you all what's inside, the element tags and namespaces
您可以从命令行使用pdb,并在文件中添加断点。但这可能涉及一些步骤
(对于windows调试,可能略有不同)
找到您的scrapy
可执行文件:
$ whereis scrapy
/usr/local/bin/scrapy
将其称为python脚本并启动pdb
$ python -m pdb /usr/local/bin/scrapy crawl quotes
进入调试器外壳后,打开另一个外壳实例并找到spider脚本的路径(位于spider项目中)
这将输出绝对路径。把它复制到你的剪贴板上
在pdb外壳类型中:
b /absolute/spider/file/path.py:line_number
…其中,行号是调试该文件时所需的断点
在调试器中点击c
现在去做一些PythonFu:)我的网站根本没有加载。response.body
是什么样子的?您可以始终包括打印站点
,并查看在爬网过程中打印的内容。此站点是我们的内部网,因此您无法访问它。如果我输入response.body,我会得到xml样式表(我不能发布太多字符)>>>response.body'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n登录名\n\n\n\n\n\n\n\n我在代码中添加了打印位置,但什么都没有发生。我在cmd提示符中看到的唯一区别是它有一个空括号[]
b /absolute/spider/file/path.py:line_number