import requests from bs4 import BeautifulSoup def getHTML(url): try: r = requests.get(url,timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "" def getLocation(url): html = getHTML(url) soup = BeautifulSoup(html,'html.parser') paras = soup.select('.topbar h2') return paras def getWeather(url): html = getHTML(url) soup = BeautifulSoup(html,'html.parser') paras = soup.select('.txt') return paras def getWet(url): html = getHTML(url) soup = BeautifulSoup(html,'html.parser') paras = soup.select('.b2') return paras def getWind(url): html = getHTML(url) soup = BeautifulSoup(html,'html.parser') paras = soup.select('.b3') return paras def saveFile(text): f=open('C:\\Users\\Administrator\\Desktop\\爬虫\\weather.txt','a') for t in text: if len(t) > 0: f.writelines(t.get_text() + "\n") f.close() def getAll(url): location = getLocation(url) saveFile(location) weather = getWeather(url) saveFile(weather) wet = getWet(url) saveFile(wet) wind = getWind(url) saveFile(wind) def main(): beijing = 'https://m.tianqi.com/beijing/' changsha = 'https://m.tianqi.com/changsha/' getAll(beijing) getAll(changsha) main()
python爬取天气预报
最新推荐文章于 2024-03-17 10:15:49 发布