gpt4 book ai didi

python - Scrapy LinkExtractor 无法找到现有的 url

转载 作者:行者123 更新时间:2023-12-01 00:30:56 26 4
gpt4 key购买 nike

我有一个这样的爬虫:

class SkySpider(CrawlSpider):
name = "spider_v1"
allowed_domains = [
"atsu.edu",
]

start_urls = [
"http://www.atsu.edu",
]

rules = (
Rule(
INFO_LINKS_EXTRACTOR,
follow=True,
callback='parse_item',
),
)
def parse_item(self, response):
print("ENTERED!")
item = SportsScraperItem()
item["contact"] = self._parse_contact(response)
return item

在我的 helpers.py 中,我有:

from scrapy.linkextractors import LinkExtractor


def _r(string):
return f"(.*?)(\b{string}\b)(.*)"


INFO_LINKS_EXTRACTOR = LinkExtractor(
allow=(
_r('about'),
),
unique=True,
)

我知道atsu.edu有一个链接https://www.atsu.edu/about-atsu/ ,但我的提取器似乎看不到它,并且 parse_item() 方法未运行。我在这里做错了什么?
编辑1:日志:

2019-10-01 15:40:58 [scrapy.core.engine] INFO: Spider opened
2019-10-01 15:40:58 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-10-01 15:40:58 [steppersspider_v1] INFO: Spider opened: steppersspider_v1
2019-10-01 15:40:58 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2019-10-01 15:40:59 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.atsu.edu/robots.txt> from <GET http://WWW.ATSU.EDU/robots.txt>
2019-10-01 15:41:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.atsu.edu/robots.txt> (referer: None)
2019-10-01 15:41:11 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.atsu.edu/> from <GET http://WWW.ATSU.EDU>
2019-10-01 15:41:15 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.atsu.edu/robots.txt> (referer: None)
2019-10-01 15:41:19 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.atsu.edu/> (referer: None)
2019-10-01 15:41:19 [steppersspider_v1] DEBUG: Saved file steppers-www.atsu.edu.html
2019-10-01 15:41:20 [scrapy.core.engine] INFO: Closing spider (finished)
2019-10-01 15:41:20 [scrapy.statscollectors] INFO: Dumping Scrapy stats:

编辑2
Here这就是我在 regexp101.com 上测试这个正则表达式的方法。

编辑3
正则表达式的工作函数:

def _r(string):
return r"^(.*?)(\b{string}\b)(.*)$".format(string=string)

最佳答案

默认情况下,链接提取器仅搜索 aarea 标记。您要查找的链接似乎位于 li 标记中。

您需要将 tags 参数与所需的标签一起传递给链接提取器的构造函数。例如:

tags=('a', 'area', 'li')

参见https://doc.scrapy.org/en/latest/topics/link-extractors.html#module-scrapy.linkextractors.lxmlhtml

关于python - Scrapy LinkExtractor 无法找到现有的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185786/

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