爬虫類プロジェクトの実際の戦闘8:這う気象条件

目的

気象インターフェースによると、来週の気象条件をクロールします。

プロジェクトの準備

ソフトウェア:Pycharm
サードパーティライブラリ:リクエスト、BeautifulSoup、csv
インターフェースアドレス:http://api.k780.com:88 /?app = weather.future&weaid = city name&appkey = 10003&sign = b59bc3ef6191eb9f747dd4e83c99f2a4&format = xml

インターフェース分析

http://api.k780.com:88/?app=weather.future&weaid=Shanghai&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml

このWebページを開きます。
ここに画像の説明を挿入

コード

import requests
from bs4 import BeautifulSoup

print('请输入城市名称:')
cityname=input()
host='http://api.k780.com:88/?app=weather.future&weaid=%s'%cityname
url=host+'&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml'
response=requests.get(url)
content=response.text
soup=BeautifulSoup(content,'lxml')
#日期
day=[]
target=soup.find_all('days')
for each in target:
    day.append(each.text)
#星期
week=[]
target=soup.find_all('week')
for each in target:
    week.append(each.text)
#城市
city=[]
target=soup.find_all('citynm')
for each in target:
    city.append(each.text)
#温度
temperature=[]
target=soup.find_all('temperature')
for each in target:
    temperature.append(each.text)
#天气状况
weather=[]
target=soup.find_all('weather')
for each in target:
    weather.append(each.text)
#风向
wind=[]
target=soup.find_all('wind')
for each in target:
    wind.append(each.text)
#风力
winp=[]
target=soup.find_all('winp')
for each in target:
    winp.append(each.text)
length=len(day)
for i in range(length):
    print(day[i],week[i],city[i],temperature[i],weather[i],wind[i],winp[i])

エフェクト表示

ここに画像の説明を挿入

ローカルに書く

    with open('F:/pycharm文件/document/data.csv', 'a', newline='') as f:
        csvwriter = csv.writer(f, delimiter=',')
        csvwriter.writerow([day[i], week[i],city[i],temperature[i],weather[i],wind[i],winp[i]])

ここに画像の説明を挿入
完全なコードは次のとおりです。

import requests
from bs4 import BeautifulSoup
import csv
print('请输入城市名称:')
cityname=input()
host='http://api.k780.com:88/?app=weather.future&weaid=%s'%cityname
url=host+'&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml'
response=requests.get(url)
content=response.text
soup=BeautifulSoup(content,'lxml')
#日期
day=[]
target=soup.find_all('days')
for each in target:
    day.append(each.text)
#星期几
week=[]
target=soup.find_all('week')
for each in target:
    week.append(each.text)
#城市
city=[]
target=soup.find_all('citynm')
for each in target:
    city.append(each.text)
#温度
temperature=[]
target=soup.find_all('temperature')
for each in target:
    temperature.append(each.text)
#天气状况
weather=[]
target=soup.find_all('weather')
for each in target:
    weather.append(each.text)
#风向
wind=[]
target=soup.find_all('wind')
for each in target:
    wind.append(each.text)
#风力
winp=[]
target=soup.find_all('winp')
for each in target:
    winp.append(each.text)
length=len(day)
for i in range(length):
    print(day[i],week[i],city[i],temperature[i],weather[i],wind[i],winp[i])
    with open('F:/pycharm文件/document/data.csv', 'a', newline='') as f:
        csvwriter = csv.writer(f, delimiter=',')
        csvwriter.writerow([day[i], week[i],city[i],temperature[i],weather[i],wind[i],winp[i]])

免責事項:あなた自身の研究と参照の使用のためだけに。

おすすめ

転載: blog.csdn.net/qq_44862120/article/details/107816865