马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
爬取百度贴吧的妹子图,没有加网页page,save哪个地方可以自己用urlretrieve,以及名字部分有更好的方法。import re
import os
import urllib.request
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/80.0.3987.132 Safari/537.36")
response = urllib.request.urlopen(req)
html = response.read()
return html
def get_img(url):
p = r'src="http:([^"]+\.jpg)"'
html = open_url(url).decode("utf-8")
imglist = re.findall(p,html)
img_list = []
for each in imglist:
img_list.append("http:" + each)
return img_list
def save_img(img_list):
for each in img_list:
num = each.rfind("/")
each_name = each[num+1:]
with open(each_name,"wb") as f:
print(each)
img = open_url(each)
f.write(img)
def download_img(folder="baidu_girls"):
os.chdir("C:\\Users\\Chysial\\Desktop")
os.mkdir(folder)
os.chdir(folder)
url = "https://tieba.baidu.com/p/6526349637"
img_list = get_img(url)
save_img(img_list)
if __name__ == "__main__":
download_img()
|