大家好,我是傲天
好,开始正题,开始我们的爬虫!
首先配上效果图
OK,先说一下我的运行环境
- Windows7
- Python3.6
接下来是依赖库
- BeautifulSoup
- requests
- pinyin
进入正题贴代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import requests import pinyin from bs4 import BeautifulSoup from os import system class Get_url_weather(object): #实现请求一个天气的URL,并对数据进行解析 def __init__(self, url, timeout=2): # 请扔进来一个url,还有一个超时查询默认为2秒吧 self.r = requests.get(url, timeout=timeout) if self.r.status_code == 404: print("出现错误,请检查输入是否正确,如若多次输入不正确,说明该程序无法查询到你地址的天气") def get(self): soup = self.get_soup() #因为我们想要的信息都在一个dl里,class="weather_info" html = self.get_dl_weather_ifno(soup) a = [] a.append("标头:{}".format(html.img["alt"])) a.append("地区:{}".format(html.dd.h2.text)) a.append("{}".format(html.find("dd", class_="kongqi").h6.text)) a.append("{}".format(html.find("dd", class_="kongqi").span.text)[:9]) a.append("{}".format(html.find("dd", class_="kongqi").span.text)[9:]) a.append("{}".format(html.find("dd", class_="shidu").b.text)) a.append("{}".format(html.find("dd", class_="shidu").find_all("b")[1].text)) a.append("{}".format(html.find("dd", class_="shidu").find_all("b")[2].text)) a.append("{}".format(html.find("dd", class_="kongqi").h5.text)) a.append("当前时间:{}".format(html.find("dd", class_="week").text)) a.append("当前天气:{}".format(html.find("span").b.text)) a.append("全天温度:{}".format(html.find("span").text)) return a def get_soup(self): return(BeautifulSoup(self.r.text, "html.parser")) def get_dl_weather_ifno(self, soup): return (soup.find("dl", attrs={'class':'weather_info'})) if __name__ == "__main__": URL = "http://www.tianqi.com/" url_path = pinyin.get(input("请输入地区名(不需要带市或省):"), format="strip") URL = URL+url_path Data = Get_url_weather(URL) data = Data.get() print('\n'.join(data)) system("pause") #print(str(pinyin.get("你好", format="strip"))) |
需要学习爬虫的 交流群:62851737
最新评论