import urllib.request
import re
def open_url(url):
req=urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6')
page=urllib.request.urlopen(req)
html=page.read().decode('utf-8')
return html
def get_img(html):
p=r'<img class="BDE_Image" src="([^"]+\.jpg)"' #加小括号效果很好,会单独返回子组
imglist=re.findall(p,html)
for each in imglist:
filename=each.split("/")[-1]
urllib.request.urlretrieve(each,filename,None)
if __name__=='__main__':
url="http://tieba.baidu.com/p/2128182108"
get_img(open_url(url))
爬虫学习:一个管用的贴吧妹子图下载器
最新推荐文章于 2024-05-02 17:43:47 发布