【问题标题】:Parsing version number from developer website using scrapy in python在python中使用scrapy从开发者网站解析版本号
【发布时间】:2018-04-05 18:33:19
【问题描述】:

我试图创建一个爬取第三方软件网站的蜘蛛,以创建当前版本号的存储库。这是我尝试从网站 css 获取当前 Firefox 版本号的脚本。我正在使用 Python 2.7

import scrapy
import html2text
from scrapy.selector import HtmlXPathSelector

class MozillaSpider(scrapy.Spider):
name = 'mozilla'
allowed_domains = ['mozilla.com']
start_urls = ['https://www.mozilla.org/en-US/firefox/notes/']

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    version = hxs.select('//html[@id="data-latest-firefox"]/text()').extract()[0]

    converter = html2text.HTML2Text()
    converter.ignore_links = True
    print(converter.handle(version))

【问题讨论】:

  • 你的问题是什么?
  • 这没有返回任何内容,我相信“版本”行是错误的,但我不知道如何

标签: python scrapy web-crawler version


【解决方案1】:

您的 xpath 表达式尝试选择 iddata-latest-firefoxhtml 元素,然后提取其中的文本。这样的元素不存在,所以你得到一个空列表。

您想要的是提取 html 元素的 data-latest-firefox 属性的值。您可以使用:

>>> response.xpath('//html/@data-latest-firefox').get()
'59.0.2'

【讨论】:

    【解决方案2】:

    你的 xpath 错误:

    //html[@id="data-latest-firefox"]/text()

    您正在尝试选择html 标记,其中id 等于data-latest-firefox 并提取其文本。没有给定id 的html标签,因此它不会返回任何内容,您需要的是:

    '/html/@data-latest-firefox'

    这意味着,选择html 标记并检索其data-latest-firefox 属性

    此外,您还可以简化您的 parse 方法:

    def parse(self, response):
        version = response.xpath('/html/@data-latest-firefox').extract_first()
        print(version)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 2018-05-30
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      相关资源
      最近更新 更多