Python爬蟲__爬取貼吧圖片和文字
阿新 • • 發佈:2018-12-30
1. 爬取圖片
1.1 前言
我當年年少,還不知道爬蟲這個東西,又想把書法圖片儲存下來,於是一張張地把圖片另存為,現在用爬蟲來爬取每一樓的書法圖片,解放一下人力:
1.2 爬取圖片的流程可以總結如下:
1)爬取網頁的html程式碼;
2)提取其中的圖片url;
3)下載圖片到本地。
1.3 程式碼
#coding:utf-8
#---------------------------------
#Created by linxiaobai 2016/09/19
#爬取百度貼吧圖片
#---------------------------------
import urllib2
import urllib
import re
#開啟貼吧的html
url="http://tieba.baidu.com/p/3825973883"
response=urllib2.urlopen(url)
html=response.read()
#提取其中所有的圖片url(使用正則)
reg=r'src="(http://imgsrc.*?\.jpg)"'
imgre=re.compile(reg)
imlist=re.findall(reg,html)
#下載圖片到本地
cnt=1
for imurl in imlist:
print cnt
print imurl
urllib.urlretrieve(imurl,"%s.jpg"%cnt);
cnt+=1
1.4 爬取結果:
2. 爬取文字
2.1 前言
http://tieba.baidu.com/p/584926093
此樓的標題是“誰來說說李清照和納蘭容若這兩人”,大致就是粉絲對兩位詞人的比較,比較有意思的是,吧主怕易安粉和容若粉打起來,還特意出來宣告“我早就說過禁止對詞人進行比較”云云……
我們要做的工作就是把每一樓發表的文字提取出來。
2.2 html格式分析
這是樓主發表的文字,html格式如下,並且其他各樓的格式也都與此一致:
可以看到每一樓的文字內容都是位於一個div中,而div的classs屬性是唯一的,因此,可以利用class屬性定位到文字的div。
res=soup.find_all('div',class_="d_post_content j_d_post_content ")
進一步,可以使用get_text()函式獲取div中的文字。
2.3 步驟梳理
綜上,可以總結出如下步驟:
1)爬取貼吧html內容;
2)獲取文字所在的div(使用BeautifulSoup);
3)獲取div中的文字
2.4 程式碼
#coding:utf-8
#---------------------------------
#Created by linxiaobai 2016/09/21
#爬取百度貼吧的文字內容
#---------------------------------
import urllib2
import re
from bs4 import BeautifulSoup
import urlparse
#修改編碼
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#1)爬取貼吧html內容
html_con=urllib2.urlopen("http://tieba.baidu.com/p/584926093").read()
#2)獲取文字所在的div(使用BeautifulSoup);
soup=BeautifulSoup(html_con,'html.parser',from_encoding='utf-8')
res=soup.find_all('div',class_="d_post_content j_d_post_content ")
#寫入檔案,寫入的標籤純屬格式需要,可以忽略
fout=open("lqz.html",'w')
fout.write("<html>")
fout.write("<head>")
fout.write("<meta charset='utf-8'/>")
fout.write("<body>")
for post in res:
fout.write("<p>")
fout.write(post.get_text())#3)獲取到div標籤下的文字內容
fout.write("</p><br/>")
fout.write("</body>")
fout.write("</head>")
fout.write("</html>")
2.5 爬取結果
尋找一下吧主害怕民眾打架,發出的警告:
2.6 程式碼優化
其實也談不上優化,因為還沒有化成oo形式,只是縮短了程式碼的長度,另外,增加爬取使用者名稱字,結果輸出到列表中。
#coding:utf-8
#---------------------------------
#Created by linxiaobai 2016/09/21
#爬取百度貼吧的文字內容
#增加爬取使用者名稱
#---------------------------------
import urllib2
import re
from bs4 import BeautifulSoup
import urlparse
#修改編碼
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#1)爬取貼吧html內容
html_con=urllib2.urlopen("http://tieba.baidu.com/p/584926093").read()
#2)獲取文字所在的div(使用BeautifulSoup);
soup=BeautifulSoup(html_con,'html.parser',from_encoding='utf-8')
res_name=soup.find_all('li',class_="d_name")
res_post=soup.find_all('div',class_='d_post_content j_d_post_content ')
#寫入檔案,寫入的標籤純屬格式需要,可以忽略
fout=open("lqz.html",'w')
fout.write("<html><head><meta charset='utf-8'/><body><table>")
cnt=1
for i in range(len(res_name)):
fout.write("<tr>")
fout.write("<td>%s</td>"%str(cnt))
fout.write("<td>%s</td>"%res_name[i].get_text())
fout.write("<td>%s</td>"%res_post[i].get_text())#3)獲取到div標籤下的文字內容
fout.write("</tr>")
cnt+=1
fout.write("<table></body></head></html>")
輸出結果。每一行的內容,從左到右依次是:序號,使用者名稱,使用者發表的文字: