【问题标题】:Using Scrapy to scrape h2 tags inside a specific class or style使用 Scrapy 抓取特定类或样式中的 h2 标签
【发布时间】:2020-10-09 07:15:26
【问题描述】:

我正在尝试抓取此网站https://www.tahko.com/fi/tapahtumat/。 我已经能够抓取主表上的事件,但我现在需要抓取每个表对应的月份。

月份(例如 Lokakuu 2020 或 Marraskuu 2020)在 h2 标签内,样式为“font-size:32px;”并且在类(这是整个 td 区域)“col-lg-8 col-md-8 col-sm-12 col-xs-12”内。

这是 HTML 代码。这被放置在一个 div 中,具有上述的类。

<h2 style="font-size:32px;">LOKAKUU 2020</h2>

这几个月我怎么刮?

到目前为止我尝试过的是:

fetch("https://www.tahko.com/fi/tapahtumat/")

full = response.xpath('//*[@class="col-lg-8 col-md-8 col-sm-12 col-xs-12"]')

months = full.xpath('/*[@style="font-size:32px;"]')

奖金问题: 将这些月份与下面的事件表相匹配的最简单方法是什么?

【问题讨论】:

  • 你的代码在哪里?
  • @baduker 我只编写了从表中抓取事件的代码。我已经编辑了到目前为止我尝试过的问题

标签: html python-3.x xpath web-scraping scrapy


【解决方案1】:

我不想建立一个完整的scrapy项目,但我希望这应该让你开始。

import requests
from lxml import html

header_month_xpath = '//*[@style="font-size:32px;"]/text()'
month_widget_xpath = '//*[@class="widget"]/a/text()'

page = requests.get("https://www.tahko.com/fi/tapahtumat/").text

print(html.fromstring(page).xpath(header_month_xpath))
print(html.fromstring(page).xpath(month_widget_xpath))

输出:

['LOKAKUU 2020', 'MARRASKUU 2020', 'JOULUKUU 2020']
['Kaikki menovinkit', 'Tammikuu 2021', 'Helmikuu 2021', 'Maaliskuu 2021', 'Huhtikuu 2021', 'Toukokuu 2021', 'Kesäkuu 2021', 'Heinäkuu 2021', 'Elokuu 2021', 'Syyskuu 2021', 'Lokakuu 2020', 'Marraskuu 2020', 'Joulukuu 2020']

【讨论】:

  • 谢谢,这最终奏效了。我首先使用full_data.xpath('//*[@style="font-size:32px;"]/text()') 将数据拆分为适当的类 div,然后使用您在上面给出的表达式得到months= full_data.xpath('//*[@style="font-size:32px;"]/text()') 这有效并给了我几个月的时间。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2017-08-02
  • 2019-04-03
  • 2018-06-30
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
相关资源
最近更新 更多