使用Scrapy选择表行

问题描述 投票:0回答:2

我知道这是一个很大的问题,但我想知道是否有人可以帮助我选择使用刮擦的物品?

我想访问下面链接的页面上的“平均温度”,我对此确实感到困惑。通常我会使用类,但是这里没有提供它们。

http://www.southamptonweather.co.uk/wxhistory.php

一如既往的谢谢,

Gus

python web-scraping scrapy
2个回答
1
投票
response.xpath('//td[text()="Average temperature"]/following-sibling::td/text()').extract()

将为您工作。

Xpaths通常比CSS选择器更强大,但编写时间也更长(:祝你好运!


1
投票

我不确定'scrapy'是什么,但是您可以轻松地从HTML表中获取项目。

# find a specific table by table count
import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("http://www.southamptonweather.co.uk/wxhistory.php")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[1]
df = pd.read_html(str(table))

结果:

[                             0                                                 1
 0   Mar 1 Average and Extremes                        Mar 1 Average and Extremes
 1          Average temperature                                             5.9°C
 2             Average humidity                                               69%
 3             Average dewpoint                                             0.6°C
 4            Average barometer                                          988.3 mb
 5            Average windspeed                                           6.9 mph
 6            Average gustspeed                                           9.7 mph
 7            Average direction                                        232° ( SW)
 8           Rainfall for month                                            2.8 mm
 9            Rainfall for year                                          267.4 mm
 10     Maximum rain per minute                    0.6 mm on day 01 at time 21:26
 11         Maximum temperature                     9.4°C on day 01 at time 14:49
 12         Minimum temperature                     3.6°C on day 01 at time 07:37
 13            Maximum humidity                       82% on day 01 at time 00:00
 14            Minimum humidity                       54% on day 01 at time 15:06
 15            Maximum pressure                  991.0 mb on day 01 at time 12:28
 16            Minimum pressure                  984.0 mb on day 01 at time 00:00
 17           Maximum windspeed                  17.3 mph on day 01 at time 10:45
 18          Maximum gust speed  28.8 mph from 225 °( SW) on day 01 at time 10:44
 19          Maximum heat index                     9.4°C on day 01 at time 14:49]

我不确定您现在想做什么。也许您想要整个桌子,如果是这样,您就完成了。如果需要数据的子集,只需对数据框应用过滤器。一种是将数据封装在数据框中,您可以做各种事情,包括重塑数据,绘制数据,统计或数学分析等。

© www.soinside.com 2019 - 2024. All rights reserved.