python簡單爬蟲代碼示例2

目標網站:view-source:http://www.weather.com.cn/weather/101270101.shtml
代碼:
from urllib.request import urlopen
from bs4 import BeautifulSoup

newshtml1=urlopen(“http://www.weather.com.cn/weather/101270101.shtml”).read() #獲取二進制文件
newshtml2=newshtml1.decode(“utf-8”)#把二進制轉爲string類型

soup=BeautifulSoup(newshtml2,features=“lxml”)#<class ‘bs4.BeautifulSoup’>
all_ul=soup.find_all(“ul”,attrs={“class”:“t clearfix”})#<class ‘bs4.element.ResultSet’>
#print(type(all_ul[0]))#<class ‘bs4.element.Tag’>
#print(len(all_ul))
f=open(“weather.html”,“w”,encoding=‘utf-8’)
f.close()
all_li=all_ul[0].find_all(“li”)#<class ‘bs4.element.ResultSet’>
f=open(“weather.html”,“a”,encoding=‘utf-8’)
for i in all_li:
data=i.find(“h1”).get_text()
#print(type(i))#<class ‘bs4.element.Tag’>
f.write(data+"\n")
data=i.find(“p”,attrs={“class”:“wea”}).get_text()
f.write(data+"\n")
data=i.find(“p”,attrs={“class”:“tem”})
print(type(data))#<class ‘bs4.element.Tag’>
pdata1=data.find(“span”).get_text()
pdata2=data.find(“i”).get_text()
f.write(pdata1+"----"+pdata2+"\n")
data=i.find(“p”,attrs={“class”:“win”})
pdata1=data.find(“i”).get_text()
f.write(pdata1+"\n"+"\n")
f.close()
#print(len(all_li))
#for i in all_ul:

print(i)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章