資源簡介
基于Python爬蟲爬取中國天氣網的天氣預報信息
代碼片段和文件信息
import?urllib.request
from?bs4?import?BeautifulSoup
url?=?“http://www.weather.com.cn/weather15d/101280601.shtml“
def?getHtml():
????response?=?urllib.request.urlopen(url)
????data?=?response.read()
????html?=?data.decode()
????return?html
def?getTemp(s):
????#?25℃/18℃
????p?=?s.find(“/“)
????max?=?s[p?-?3:len(s)?-?5]
????min?=?s[p?+?1:len(s)?-?1]
????return?{“max“:?max?“min“:?min}
html?=?getHtml()
#?創建一個對象,使用xml解析
root?=?BeautifulSoup(html?“lxml“)
#?select_one使用CSS語法查找ul元素
- 上一篇:python函數編程和講解
- 下一篇:神經網絡用于分類
評論
共有 條評論