gpt4 book ai didi

python - 使用 Python 和 Scrapy 递归抓取 : Information Not Retrieved

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:36 25 4
gpt4 key购买 nike

我正在尝试使用 Scrapy 从 Pratt 网站上提取联系信息,但未检索到信息。我的代码如下:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector, Selector
from scrapy.http import Request

class ESpider(CrawlSpider):
name = "pratt"
allowed_domains = ["pratt.edu"]
start_urls = ["https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302"]

rules = (Rule (SgmlLinkExtractor(restrict_xpaths=('/html/body/div[3]/div/div[2]/div/div/p/a',))
, callback="parse_items", follow= True),
)

def parse_items(self, response):
contacts = Selector(response)
print contacts.xpath('/html/body/div[3]/div/div[2]/table/tbody/tr[2]/td[2]/h3').extract()
print contacts.xpath('/html/body/div[3]/div/div[2]/table/tbody/tr[2]/td[2]/a').extract()

从我的 start_url 开始,我想遍历每个人的链接并从下一页获取他们的姓名和电子邮件地址。当我运行我的抓取工具时,我收到以下输出:

2014-03-10 16:46:37-0400 [scrapy] INFO: Scrapy 0.22.2 started (bot: emailSpider)
2014-03-10 16:46:37-0400 [scrapy] INFO: Optional features available: ssl, http11
2014-03-10 16:46:37-0400 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'emailSpider.spiders', 'SPIDER_MODULES': ['emailSpider.spiders'], 'DEPTH_LIMIT': 1, 'BOT_NAME': 'emailSpider'}
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled item pipelines:
2014-03-10 16:46:37-0400 [pratt] INFO: Spider opened
2014-03-10 16:46:37-0400 [pratt] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-03-10 16:46:37-0400 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2014-03-10 16:46:37-0400 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2014-03-10 16:46:41-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302> (referer: None)
2014-03-10 16:46:44-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/bio/?id=eabruzzo> (referer: https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302)
[]
[]
2014-03-10 16:46:47-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/bio/?id=ehinrich> (referer: https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302)
[]
[]

(我在几次迭代后停止了程序)。看起来这些页面正在被抓取,但返回的是空列表。知道为什么会这样吗?非常感谢。

最佳答案

您的 xpath 不正确。而且,实际上,它们非常脆弱,请尽量避免在爬虫中使用绝对 xpath。最好找到一个具有唯一 ID 或类名的容器并依赖它。

这是您的蜘蛛的固定版本(依赖于 id="content"div):

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.item import Item, Field
from scrapy.selector import Selector


class PrattItem(Item):
name = Field()
email = Field()


class ESpider(CrawlSpider):
name = "pratt"
allowed_domains = ["pratt.edu"]
start_urls = ["https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302"]

rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//div[@id="content"]',)), callback="parse_items", follow=True), )

def parse_items(self, response):
contacts = Selector(response)

item = PrattItem()
item['name'] = contacts.xpath('//div[@id="content"]/table//h2/text()').extract()[0]
item['email'] = contacts.xpath('//div[@id="content"]/table/tr[2]/td[2]/a/@href').extract()[0]
return item

希望对您有所帮助。

关于python - 使用 Python 和 Scrapy 递归抓取 : Information Not Retrieved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311229/

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