python – Scrapy不会抓取所有页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Scrapy不会抓取所有页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2202字,纯文字阅读大概需要4分钟。
内容图文
![python – Scrapy不会抓取所有页面](/upload/InfoBanner/zyjiaocheng/722/971ba03976bd4ddebd8d1645ad69632b.jpg)
这是我的工作代码:
from scrapy.item import Item, Field
class Test2Item(Item):
title = Field()
from scrapy.http import Request
from scrapy.conf import settings
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
class Khmer24Spider(CrawlSpider):
name = 'khmer24'
allowed_domains = ['www.khmer24.com']
start_urls = ['http://www.khmer24.com/']
USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 AlexaToolbar/alxg-3.1"
DOWNLOAD_DELAY = 2
rules = (
Rule(SgmlLinkExtractor(allow=r'ad/.+/67-\d+\.html'), callback='parse_item', follow=True),
)
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
i = Test2Item()
i['title'] = (hxs.select(('//div[@class="innerbox"]/h1/text()')).extract()[0]).strip(' \t\n\r')
return i
它只能废弃10或15条记录.总是随机数!我无法设法获得具有像http://www.khmer24.com/ad/any-words/67-anynumber.html这样的模式的所有页面
我真的怀疑Scrapy因为重复请求而完成了爬行.他们建议使用dont_filter = True但是,我不知道将它放在我的代码中的哪个位置.我是Scrapy的新手,真的需要帮助.
解决方法:
1.“他们建议使用dont_filter = True但是,我不知道将它放在我的代码中的哪个位置.”
这个参数在BaseSpider中,CrawlSpider继承自. (scrapy/spider.py)默认设置为True.
“它只能废弃10或15条记录.”
原因:
这是因为start_urls不是那么好.
在这个问题中,蜘蛛在http://www.khmer24.com/开始爬行,让我们假设它有10个url跟随(它满足模式).
然后,蜘蛛继续爬行这10个网址.但是由于这些页面包含如此少的满意模式,蜘蛛会得到一些网址(甚至没有网址),这会导致停止爬行.
可能的方法:
我上面所说的原因只是重申了icecrime的观点.解决方案也是如此.
>建议使用’所有广告’页面作为start_urls. (您也可以将主页用作start_urls并使用新规则.)
>新规则:
rules = (
# Extract all links and follow links from them
# (since no callback means follow=True by default)
# (If "allow" is not given, it will match all links.)
Rule(SgmlLinkExtractor()),
# Extract links matching the "ad/any-words/67-anynumber.html" pattern
# and parse them with the spider's method parse_item (NOT FOLLOW THEM)
Rule(SgmlLinkExtractor(allow=r'ad/.+/67-\d+\.html'), callback='parse_item'),
)
参考:
SgmlLinkExtractor,
CrawlSpider example
内容总结
以上是互联网集市为您收集整理的python – Scrapy不会抓取所有页面全部内容,希望文章能够帮你解决python – Scrapy不会抓取所有页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。