#pywsh.py,上海空气质量报告
import requests
from lxml import etree
url = "https://data.rmtc.org.cn/gis/listsation0_94M.html"
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
with requests.request('GET',url,headers = {'User-agent':ua}) as res:
content = res.text
#print(content)
html = etree.HTML(content)
shdiv = html.xpath("//div[@class='main']//text()")
for i in shdiv:
s=i.strip()
if len(s)>0:print(s)
divnames = html.xpath("//div[@class='main']//li[@class='datali']//div[@class='divname']//text()")
divvals = html.xpath("//div[@class='main']//li[@class='datali']//span[@class='label']//text()")
divdates = html.xpath("//div[@class='main']//li[@class='datali']//span[@class='showtime']//text()")
sdivnames=[]
sk=0
for i in divnames:
s=i.strip()
if len(s)>0:
sk+=1
sdivnames.append(s)
ans = dict(zip(sdivnames,divvals))
sk=0
print("-"*25)
for k,v in ans.items():
k=k.strip()
v=v.strip()
print(k,v,divdates[sk])
sk+=1
print("-"*25)
for i in range(4):
print(sdivnames[i].strip(),divvals[i].strip(),divdates[i].strip())
#命令行执行 python pywsh.py
|