VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python3 >
  • python3教程之《爬虫学习》(五)(爬虫实战之爬取天气信息)

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


1.大体框架列出+爬取网页:

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
#数据可视化
from pyecharts import Bar
#用来url连接登陆等功能
import requests
#解析数据
from bs4 import BeautifulSoup
 
#用来存取爬取到的数据
data = []
 
def parse_data(url):
    headers = {
        'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400"
    }
    rest = requests.get(url=url, headers=headers)#使用requests.get方法爬取网页
    # 一般人可能会用rest.text,但是会显示乱码
    text = rest.content.decode('utf-8')#使用utf-8解码,防止显示乱码,接下来无法解析
    soup = BeautifulSoup(text, 'html5lib')#BeautifulSoup方法需要指定解析文本和解析方式
 
 
def main():
    url = "http://www.weather.com.cn/textFC/hb.shtml"
    parse_data(url)
 
if __name__ == '__main__':
    main()<br data-filtered="filtered"><br data-filtered="filtered">
相关教程