由于项目原因,需要简单快速地抓取上海浦东新区未来一天的天气情况,于是参考了博主@双木已成林的这篇文章,进行修改而生成,感谢!
url_address = 'http://www.weather.com.cn/weather/101020600.shtml'
打开url_address,出现的是未来7天上海市浦东新区的天气情况,其中,101020600是代表上海市的编号。
url_address = 'http://www.weather.com.cn/weather/101020600.shtml'
resq = request.Request(url_address)
resq.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
with request.urlopen(resq) as html:
data = html.read().decode("utf-8")
soup=BeautifulSoup(data,'html.parser')
soup可以把页面整齐地解析出来,利用print(soup&