爬虫天气预报 XPATH
时间: 2023-11-25 09:51:28 浏览: 93
aaaaaaaaaaaxpath.zip
以下是使用XPath爬取天气预报的示例代码:
```python
import requests
from lxml import etree
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求
url = 'http://www.weather.com.cn/weather/101280101.shtml'
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
# 解析HTML
selector = etree.HTML(response.text)
# 获取天气信息
WeatherLink = selector.xpath('//*[@id="7d"]/ul/li')
WeatherList = []
for i in range(len(WeatherLink)):
date = WeatherLink[i].xpath('./h1/text()')[0]
weather = WeatherLink[i].xpath('./p[@class="wea"]/text()')[0]
temp = WeatherLink[i].xpath('./p[@class="tem"]/span/text()')[0] + '/' + WeatherLink[i].xpath(
'./p[@class="tem"]/i/text()')[0]
wind = WeatherLink[i].xpath('./p[@class="win"]/i/text()')[0]
WeatherList.append([date, weather, temp, wind])
# 打印天气信息
for weather in WeatherList:
print(weather)
```
阅读全文