使用pip安装相关依赖:
pip install requests
pip install bs4
安装成功提示:Successfully installed *...
爬取中国天气网数据示例代码:
#-*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup if __name__ == '__main__': url = 'http://lishi.tianqi.com/wuhan/201806.html' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') weather_div = soup.select('div[class="tqtongji2"]')[0] ul_list = weather_div.select('ul') for ul in ul_list: li_list = ul.select('li') print("日期:%s 最高气温:%s 最低气温:%s 天气:%s 风向:%s 风力:%s " % (li_list[0].string.encode('utf-8'), li_list[1].string.encode('utf-8'), li_list[2].string.encode('utf-8'), li_list[3].string.encode('utf-8'), li_list[4].string.encode('utf-8'), li_list[5].string.encode('utf-8')) )
执行代码结果: