python 近一周
时间: 2023-11-05 20:59:51 浏览: 87
近一周的天气数据可以通过以下代码获取:
```python
import requests
import pandas as pd
from lxml import etree
url = "http://tianqi.2345.com/today-71989.htm"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
response = requests.get(url, headers=headers)
html = etree.HTML(response.text)
divs = html.xpath('//div[@class="seven-day"]')[0]
lis = divs.xpath('./ul/li/a')
dates = []
weathers = []
temps = []
winds = []
air_qua = []
for li in lis[1:]:
dates.append(li.xpath('./em/text()')[0])
weathers.append(li.xpath('./i/text()')[0])
temps.append(li.xpath('./span[@class="tem-show"]/text()')[0])
winds.append(li.xpath('./span[@class="wind-name"]/text()')[0])
air_qua.append(li.xpath('./span[@class="wea-qulity"]/text()')[0])
datas = {"日期": dates, "天气": weathers, "温度": temps, "风力": winds, "空气质量": air_qua}
df = pd.DataFrame(datas)
```
阅读全文