python爬虫(1)-使用requests和beautifulsoup库爬取中国天气网
使用工具及准备
python3.7(python3以上都可以)
pycharm IDE (笔者习惯使用pycharm,也可以使用其他的)
URL: http://www.weather.com.cn/weather/101230101.shtml
beautifulsoup4、requests、lxml库(pip3 install beautifulsoup4,pip3 install requests)
重点来了
(1)因为beautifulsoup要传入的是Unicode类型数据,所以首先要得到网页的纯文本,这就会用到requests库了,先利用requests的text得到纯文本
利用requests的get()函数发送get请求,get函数可以接受数个参数,详情看http://docs.python-requests.org/zh_CN/latest/user/quickstart.html,我们这里就用两个参数,URL(请求地址)以及headers(特别注意,requests库默认的编码方式是Unicode编码,但是此网站是utf-8编码,所以要指定一下编码方式)
import requests
url =