python-简单爬虫抓取贴吧图片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-简单爬虫抓取贴吧图片相关的知识,希望对你有一定的参考价值。

一个简单的爬虫练习
import urllib.request
import re

def getHtml(url):
    page = urllib.request.urlopen(url)
    #read()返回的结果是二进制的需要转换成字符串
    html =page.read().decode("utf-8")
    return html

def getImage(page):

    reg=rsrc="(.+?\.jpg)" size
    imgre=re.compile(reg)
    print (imgre)
    imglist=re.findall(imgre,page)
    print (imglist)
    x=0
    for i in imglist:
        urllib.request.urlretrieve(i,%s.jpg %x)
        x+=1
    return imglist

html=getHtml("http://tieba.baidu.com/p/4721099001")
print(getImage(html))

 

以上是关于python-简单爬虫抓取贴吧图片的主要内容,如果未能解决你的问题,请参考以下文章