gpt4 book ai didi

python - Scrapy SgmlLinkExtractor 忽略允许的链接

转载 作者:太空狗 更新时间:2023-10-29 18:29:18 24 4
gpt4 key购买 nike

请看this spider example在 Scrapy 文档中。解释是:

This spider would start crawling example.com’s home page, collecting category links, and item links, parsing the latter with the parse_item method. For each item response, some data will be extracted from the HTML using XPath, and a Item will be filled with it.

我完全复制了同一个蜘蛛,并用另一个初始 url 替换了“example.com”。

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from stb.items import StbItem

class StbSpider(CrawlSpider):
domain_name = "stb"
start_urls = ['http://www.stblaw.com/bios/MAlpuche.htm']

rules = (Rule(SgmlLinkExtractor(allow=(r'/bios/.\w+\.htm', )), callback='parse', follow=True), )

def parse(self, response):
hxs = HtmlXPathSelector(response)

item = StbItem()
item['JD'] = hxs.select('//td[@class="bodycopysmall"]').re('\d\d\d\d\sJ.D.')
return item

SPIDER = StbSpider()

但是我的蜘蛛“stb”并没有像它应该做的那样从“/bios/”收集链接。它运行初始 url,抓取 item['JD'] 并将其写入文件,然后退出。

为什么 SgmlLinkExtractor 被忽略了? Rule 被读取,因为它捕获 Rule 行内的语法错误。

这是一个错误吗?我的代码有问题吗?除了我在每次运行时看到的一堆未处理的错误外,没有任何错误。

很高兴知道我在这里做错了什么。感谢您提供任何线索。我是否误解了 SgmlLinkExtractor 应该做什么?

最佳答案

parse 函数实际上是在 CrawlSpider 类中实现和使用的,您无意中覆盖了它。如果您将名称更改为其他名称,例如 parse_item,那么该规则应该有效。

关于python - Scrapy SgmlLinkExtractor 忽略允许的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1811132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com