 import urllib.request
import re
import os
import random
from bs4 import BeautifulSoup
def request_url():
url='https://www.tooopen.com/topiclist/9620.aspx'
response =urllib.request.Request(url)
html = urllib.request.urlopen(response)
#html.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')
html = html.read().decode('utf-8')
reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
photo_url= re.findall(reg,html)
#print(photo_url[0][0])
for i in photo_url:
#urls =i[0]
#name =i[1]
urls,name =i
r =urllib.request.urlopen(urls).read().decode('utf-8')
#print(r)
soup =BeautifulSoup(r,"html.parser")
img_url = soup.find("div",class_="img-list").find_all("a")
reg=r'http(.*?)jpg'
link = re.findall(reg,str(img_url))
for i in link:
i='http'+i+'jpg'
print(i)
download_imgs = urllib.request.urlopen(i).read()
res = urllib.request.urlopen(i)
data = res.read()
with open(f'{random.randint(1, 1000)}.jpg','wb') as f:
f.write(data)
request_url()
|