import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = re.compile('<br /><img\ssrc=(.*?)\sreferrerPolicy')
#p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p,html)
save_img(img_list)
def save_img(img_list):
for each in img_list:
#print(each)
filename = each.split("/")[-1]
with open(filename, 'wb') as f:
img = open_url(each)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url))
|