阅读背景:

爬取中国天气_henusyb的博客

来源:互联网 

中国天气网爬取之华北城市数据爬取

import requests
from bs4 import BeautifulSoup

def parser_page(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
    }
    response = requests.get(url,headers=headers)
    text = response.content.decode('utf-8')
    # return text
    soup = BeautifulSoup(text,'lxml')
    conMidtab = soup.find('div',attrs={"class":"conMidtab"})#获取某个标签下的某个属性
    tables = conMidtab.find_all('table')
    for table in tables:
        trs = table.find_all('tr')[2:]
        for tr in trs:
            tds = tr.find_all('td')
            city_td = tds[0]
            city = list(city_td.stripped_strings)[0]#获取值所有子孙节点文本的第一个
            temp_td = tds[-2]
            min_temp = list(temp_td.stripped_strings)[0]
            print(city,min_temp)

def main():
    url = 'https://www.weather.com.cn/textFC/hb.shtml'
    parser_page(url)

if __name__ == '__main__':
    main()
import requests
from 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: