爬取各城市的天气
参考的数据来源:http://www.weather.com.cn/textFC/hb.shtml
一、BeautifulSoup
这个方法最简单
import requests
import csv
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
}
class WeatherSpider():
def __init__(self):
self.start = 0
def getSource(self,page):
url = 'http://www.weather.com.cn/textFC/'+page+'.shtml'
wb_data = requests.get(url,headers=headers).content.decode('utf-8')
return wb_data
def parseSource(self,page):
soup = BeautifulSoup(self.getSource(page),'lxml')
com = soup.find_all('div',class_="conMidtab")[1] # 1代表的是明天。0~6可以选择7天的天气
tables = com.find_all('table')
onerows = []
for t