《纽约时报》今日头条

2024-05-16 19:56:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近才开始学“刮痧”,我选择了《纽约时报》的每日词汇作为第一个测试。https://www.nytimes.com/column/learning-word-of-the-day

我注意到他们有一个API,但就我的情况而言,它没有任何我可以使用的(我想)。我基本上是在页面上浏览一天中的每一个单词,然后检索单词、意思和示例段落。在

这段简短的代码应该遍历每个url并检索至少一个单词,但是我得到了一大堆错误,我不知道为什么! 我一直在使用SelectorGadget获取我需要的CSS代码,到目前为止,这是我的代码:

import scrapy

class NewYorkSpider(scrapy.Spider):
    name = "times"
    start_urls = [ "https://www.nytimes.com/column/learning-word-of-the-day" ]

    # entry point for the spider
    def parse(self,response):
        for href in response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "headline", " " ))]'):
            url = href.extract()
            yield scrapy.Request(url, callback=self.parse_item)

    def parse_item(self, response):
        word = response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "story-subheading", " " ))]//strong').extract()[0]

谢谢你,很多!在

更新的错误(现在不完全是错误,只是没有抓取假定的信息):

^{pr2}$

Tags: the代码httpsselfurlparseresponsewww
2条回答

您正在.css方法中使用xpath表达式,该方法用于css选择器表达式。
只需将.css替换为.xpath

response.css('//*[contains(concat( " ", @class, " " ), concat( " ", "headline", " " ))]')
# to
response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "headline", " " ))]')

关于第二个错误-提取的url不是绝对url,例如/some/sub/page.html。要将其转换为绝对url,可以使用response.urljoin()函数:

^{pr2}$

关于你的第三个错误-你的xpath在这里有问题。看起来您使用了一些xpath生成器,这些东西很少生成任何有价值的东西。您在这里寻找的只是一个<a>节点和story-link类:

^{3}$

对于您的单词xpath,您只需在node下使用文本,该节点位于:

word = response.xpath("//h4/strong/text()").extract_first()

这个代码应该有效。为了从每个单词的网站上获取所需的其他信息,只需将适当的选择器与XPath或CSS表达式结合使用。在

关于选择器的更多信息,我推荐this站点,当然还有{a2}。在

import scrapy

class NewYorkSpider(scrapy.Spider):
    name = "times"
    start_urls = ["https://www.nytimes.com/column/learning-word-of-the-day"]

    # entry point for the spider
    def parse(self,response):
        for href in response.css('a[class="story-link"]::attr(href)'):
            yield scrapy.Request(href.extract(), callback=self.parse_item)

    def parse_item(self, response):
        heading = response.css('h4[class="story-subheading story-content"] strong::text').extract_first()

相关问题 更多 >