Pey*_*man 2 python xpath web-crawler scrapy
在使用 Scrapy 1.6抓取页面https://github.com/rg3/youtube-dl/pull/11272时,当我使用选择器进行选择时:
//div[@class = 'file js-comment-container js-resolvable-timeline-thread-container']
如果你在浏览器或其他东西上使用它,不要忘记阻止 JS。
结果将是没有属性“extract_first”的东西。
例如,运行此代码将面临该错误:
for code_and_comment in response.xpath(
"//div[@class = 'file js-comment-container js-resolvable-timeline-thread-container']"):
if code_and_comment is None:
print('it is NONE')
print(code_and_comment.extract_first())
Run Code Online (Sandbox Code Playgroud)
我无法理解,你知道我错在哪里吗?提前致谢。
ROBOTSTXT_OBEY = False原因是在您的代码code_and_comment中已经是一个Selector,因此没有必要使用extract_first. 它仅适用于选择器列表(这是您从中获得的response.xpath(...))。
您只需执行以下操作:
for code_and_comment in response.xpath(
"//div[@class = 'file js-comment-container js-resolvable-timeline-thread-container']"):
if code_and_comment is None:
print('it is NONE')
print(code_and_comment.extract())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |