用到了urllib2 json 很簡(jiǎn)單的一個(gè)應(yīng)用 如下
為黃州等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及黃州網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、黃州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!獲取城市編號(hào)
#coding=utf-8 import urllib2 url1 = 'http://m.weather.com.cn/data3/city.xml' content1 = urllib2.urlopen(url1).read() provinces = content1.split(',') print content1 # 輸出content1可以查看全部省份代碼 result = '' url = 'http://m.weather.com.cn/data3/city%s.xml' for p in provinces: p_code = p.split('|')[0] url2 = url % p_code content2 = urllib2.urlopen(url2).read() # 輸出content2可以查看此省份下所有城市代碼 cities = content2.split(',') print content2 for c in cities: c_code = c.split('|')[0] url3 = url % c_code content3 = urllib2.urlopen(url3).read() print content3 #content3是此城市下所有地區(qū)代碼 districts = content3.split(',') for d in districts: # 對(duì)于每個(gè)地區(qū),我們把它的名字記錄下來(lái),然后再發(fā)送一次請(qǐng)求,得到它的最終代碼: d_pair = d.split('|') d_code = d_pair[0] # if 5 == len(d_code): continue temp=[d_code] temp.insert(4,0) d_code ="".join(temp) name = d_pair[1] # 名字 url4 = url % d_code content4 = urllib2.urlopen(url4).read() print content4 code = content4.split('|')[1] line = "%s:%s\n" % (name, code) result += line print name + ':' + code f = file('./city', 'w') f.write(result) f.close()
當(dāng)前名稱:python實(shí)現(xiàn)自主查詢實(shí)時(shí)天氣-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article48/cogiep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作、建站公司、App開發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容