爬百度贴吧图片

import urllib.request
import re

def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')

return (html)

def get_img(html):
p = r' imglist = re.findall(p,html)

#for each in imglist:
#    print(each)
for each in imglist:
    filename = each.split("/")[-1]
    urllib.request.urlretrieve(each,filename,None)

if name == 'main':
url = "https://tieba.baidu.com/p/6389247982"
get_img(open_url(url))

你可能感兴趣的:(爬百度贴吧图片)