python 爬取天氣

2022-09-02 09:48:08 字數 726 閱讀 4467

準備工作做好了,接下來就是**了

用py爬天氣資訊,需要使用兩個模組,分別是urllib2(獲取資料)和json(解析資料)

#coding=utf-8

import urllib2

import json

from city import city

cityname= raw_input('你想查哪個城市的天氣?\n')

citycode= city.get(cityname)

if citycode:

# url = (""%citycode)

url=(""%citycode)

try:

html = urllib2.urlopen(url)

content = html.read()

# print(content)

map = json.loads(content)

weatherinfo = map['weatherinfo']

str_temp = ("%s\n%s-%s") % (weatherinfo['weather'], weatherinfo['temp1'], weatherinfo['temp2'])

print(str_temp)

except:

print("error")

else:

print("查無此城市")

Python 爬取天氣資訊

第一次python部落格,僅作紀念。import requests import re from bs4 import beautifulsoup requests庫從網上獲取資源,re bs4 庫,用來提取需要的資訊。開啟要爬取的 右擊檢視其源 找到感興趣的內容,如下 2020年01月12日 星期...

使用Python 爬取歷史天氣

最近做個簡單小專案需要一些天氣資料值,所以想去網上爬取一些。下面是我的爬取案例。使用的 是2345天氣網。2.xpath表示式 3.取文字 import requests from lxml import etree defget table url headers 請求資料 並獲得返回的json資...

爬取中國天氣

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