Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告_第1頁
Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告_第2頁
Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告_第3頁
Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告_第4頁
Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)報(bào)告Python網(wǎng)絡(luò)爬蟲實(shí)習(xí)(報(bào)告)-13-目錄一、選題背景 -2-二、爬蟲原理 -2-三、爬蟲歷史和分類 -2-四、常用爬蟲框架比較 -5-五、數(shù)據(jù)爬取實(shí)戰(zhàn)(豆瓣網(wǎng)爬取電影數(shù)據(jù)) -6-1分析網(wǎng)頁 -6-2爬取數(shù)據(jù) -7-3數(shù)據(jù)整理、轉(zhuǎn)換 -10-4數(shù)據(jù)保存、展示 -12-5技術(shù)難點(diǎn)關(guān)鍵點(diǎn) -12-六、總結(jié) -14-

選題背景爬蟲原理爬蟲歷史和分類常用爬蟲框架比較Scrapy框架:Scrapy框架是一套比較成熟的Python爬蟲框架,是使用Python開發(fā)的快速、高層次的信息爬取框架,可以高效的爬取web頁面并提取出結(jié)構(gòu)化數(shù)據(jù)。Scrapy應(yīng)用范圍很廣,爬蟲開發(fā)、數(shù)據(jù)挖掘、數(shù)據(jù)監(jiān)測(cè)、自動(dòng)化測(cè)試等。Crawley框架:Crawley也是Python開發(fā)出的爬蟲框架,該框架致力于改變?nèi)藗儚幕ヂ?lián)網(wǎng)中提取數(shù)據(jù)的方式。Portia框架:Portia框架是一款允許沒有任何編程基礎(chǔ)的用戶可視化地爬取網(wǎng)頁的爬蟲框架。newspaper框架:newspaper框架是一個(gè)用來提取新聞、文章以及內(nèi)容分析的Python爬蟲框架。Python-goose框架:Python-goose框架可提取的信息包括:<1>文章主體內(nèi)容;<2>文章主要圖片;<3>文章中嵌入的任heYoutube/Vimeo視頻;<4>元描述;<5>元標(biāo)簽五、數(shù)據(jù)爬取實(shí)戰(zhàn)(豆瓣網(wǎng)爬取電影數(shù)據(jù))1分析網(wǎng)頁#獲取html源代碼

def__getHtml():

data=[]

pageNum=1

pageSize=0

try:

while(pageSize<=125):

#headers={'User-Agent':'Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.11(KHTML,likeGecko)Chrome/23.0.1271.64Safari/537.11',

#'Referer':None#注意如果依然不能抓取的話,這里可以設(shè)置抓取網(wǎng)站的host

#}

#opener=urllib.request.build_opener()

#opener.addheaders=[headers]

url="/top250?start="+str(pageSize)+"&filter="+str(pageNum)

#data['html%s'%i]=urllib.request.urlopen(url).read().decode("utf-8")

data.append(urllib.request.urlopen(url).read().decode("utf-8"))

pageSize+=25

pageNum+=1

print(pageSize,pageNum)

exceptExceptionase:

raisee

returndata2爬取數(shù)據(jù)def__getData(html):

title=[]#電影標(biāo)題

#rating_num=[]#評(píng)分

range_num=[]#排名

#rating_people_num=[]#評(píng)價(jià)人數(shù)

movie_author=[]#導(dǎo)演

data={}

#bs4解析html

soup=BeautifulSoup(html,"html.parser")

forliinsoup.find("ol",attrs={'class':'grid_view'}).find_all("li"):

title.append(li.find("span",class_="title").text)

#rating_num.append(li.find("div",class_='star').find("span",class_='rating_num').text)

range_num.append(li.find("div",class_='pic').find("em").text)

#spans=li.find("div",class_='star').find_all("span")

#forxinrange(len(spans)):

#ifx<=2:

#pass

#else:

#rating_people_num.append(spans[x].string[-len(spans[x].string):-3])

str=li.find("div",class_='bd').find("p",class_='').text.lstrip()

index=str.find("主")

if(index==-1):

index=str.find("...")

print(li.find("div",class_='pic').find("em").text)

if(li.find("div",class_='pic').find("em").text==210):

index=60

#print("aaa")

#print(str[4:index])

movie_author.append(str[4:index])

data['title']=title

#data['rating_num']=rating_num

data['range_num']=range_num

#data['rating_people_num']=rating_people_num

data['movie_author']=movie_author

returndata3數(shù)據(jù)整理、轉(zhuǎn)換def__getMovies(data):

f=open('F://douban_movie.html','w',encoding='utf-8')

f.write("<html>")

f.write("<head><metacharset='UTF-8'><title>Inserttitlehere</title></head>")

f.write("<body>")

f.write("<h1>爬取豆瓣電影</h1>")

f.write("<h4>作者:劉文斌</h4>")

f.write("<h4>時(shí)間:"+nowtime+"</h4>")

f.write("<hr>")

f.write("<tablewidth='800px'border='1'align=center>")

f.write("<thead>")

f.write("<tr>")

f.write("<th><fontsize='5'color=green>電影</font></th>")

#f.write("<thwidth='50px'><fontsize='5'color=green>評(píng)分</font></th>")

f.write("<thwidth='50px'><fontsize='5'color=green>排名</font></th>")

#f.write("<thwidth='100px'><fontsize='5'color=green>評(píng)價(jià)人數(shù)</font></th>")

f.write("<th><fontsize='5'color=green>導(dǎo)演</font></th>")

f.write("</tr>")

f.write("</thead>")f.write("<tbody>")

fordataindatas:

foriinrange(0,25):

f.write("<tr>")

f.write("<tdstyle='color:orange;text-align:center'>%s</td>"%data['title'][i])

#f.write("<tdstyle='color:blue;text-align:center'>%s</td>"%data['rating_num'][i])

f.write("<tdstyle='color:red;text-align:center'>%s</td>"%data['range_num'][i])

#f.write("<tdstyle='color:blue;text-align:center'>%s</td>"%data['rating_people_num'][i])

f.write("<tdstyle='color:black;text-align:center'>%s</td>"%data['movie_author'][i])

f.write("</tr>")

f.write("</tbody>")f.write("</thead>")

f.write("</table>")

f.write("</body>")

f.write("</html>")

f.close()

if__name__=='__main__':

datas=[]

htmls=__getHtml()

foriinrange(len(htmls)):

data=__getData(htmls[i])

datas.append(data)

__getMovies(datas)4數(shù)據(jù)保存、展示結(jié)果如后圖所示:5技術(shù)難點(diǎn)關(guān)鍵點(diǎn)數(shù)據(jù)爬取實(shí)戰(zhàn)(搜房網(wǎng)爬取房屋數(shù)據(jù))frombs4importBeautifulSoup

importrequests

rep=requests.get('/top/')

rep.encoding="gb2312"#設(shè)置編碼方式

html=rep.text

soup=BeautifulSoup(html,'html.parser')

f=open('F://fang.html','w',encoding='utf-8')

f.write("<html>")

f.write("<head><metacharset='UTF-8'><title>Inserttitlehere</title></head>")

f.write("<body>")

f.write("<center><h1>新房成交TOP3</h1></center>")

f.write("<tableborder='1px'width='1000px'height='800px'align=center><tr>")

f.write("<th><h2>房址</h2></th>")

f.write("<th><h2>成交量</h2></th>")

f.write("<th><h2>均價(jià)</h2></th></tr>")

forliinsoup.find("ul",class_="ul02").find_all("li"):

name=li.find("div",class_=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論