我这里抓取的百度贴吧的地址是http://tieba.baidu.com/p/2460150866?pn=1。以下是源码,使用的是python2。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import re
import urllib
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r 'src="(.+?\.jpg)" pic_ext'
imgre = re. compile (reg)
imglist = imgre.findall(html)
x = 0
for imgurl in imglist:
print (imgurl)
urllib.urlretrieve(imgurl,r 'C:\Users\Water\PycharmProjects\test\image\%s-%s.jpg' % (i,x))
x = x + 1
i = 1
while i < 74 :
html = getHtml( "http://tieba.baidu.com/p/2460150866?pn=" + str (i))
getImg(html)
i + = 1
print (i)
|
下面是抓取的结果

本文转自 wzlinux 51CTO博客,原文链接:http://blog.51cto.com/wzlinux/1788735,如需转载请自行联系原作者