beautiful 獲取中國天氣網資料

2021-10-23 01:24:15 字數 1230 閱讀 1385

import re

import os

import time

import pandas as pd

import requests

from bs4 import beautifulsoup

import random

# 完整**

url = ''

# proxy = [, , ]

response = requests.get(url,headers = header) #proxies = random.choice(proxy)

response.encoding = 'utf-8'

soup = beautifulsoup(response.text,'lxml') # 對html進行解析,完成初始化

# print(soup.prettify()) # 字串按照標準縮排格式輸出,自動進行格式更正

ul_tag = soup.find(name='ul', class_ = 't clearfix')

li_tag = ul_tag.find_all(name = 'li')

datalist =

for tag in li_tag:

try:

# 日期

date = tag.find(name='h1').text

# 天氣

weather = tag.find(name='p' , class_='wea').text

#最高溫,最低溫

# maxc = tag.find('p', 'tem').span.text

minc = tag.find(name ='p', class_= 'tem').i.text

# 風向1

win = tag.find(name ='p', class_='win').span['title']

# 風力

seed = tag.find(name ='p', class_='win').i.text

except exception as e:

print(e)

break

df = pd.dataframe(datalist,columns=['日期', '天氣', '最低溫', '風向', '風力'])

df.to_excel('./中國天氣.xls',index = false)

C 獲取中國天氣網免費天氣預報資訊

中國天氣網介面位址 weathercitycode 為城市code 獲取的資訊為xml資料,所以在使用前我們可以將它轉化成字串,之後在序列化成天氣類 string weatherinfourl weathercitycode string weatherstr gethtml2 weatherinf...

爬取中國天氣

import requests from bs4 import beautifulsoup def parser page url headers response requests.get url,headers headers text response.content.decode utf 8...

關於中國天氣網API

上次做飛信天氣預報的時候簡單搜尋了一下中國天氣網的 api 當然是沒有官方文件的,最初是誰挖掘出來的也不可考了。剛才在增加新功能的時候又順手搜了一下相關資料,發現這個 api 還真不簡單。中國天氣網有三個 api 適用於不同場合的使用。這個介面返回的格式如下。上面這個就是我現在在用的,返回的資料最全...