1. 程式人生 > >python簡單爬蟲程式碼示例2

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)