|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
import re
import os
import time
down_path = './azhu'
if not os.path.exists(down_path):
os.makedirs(down_path)
def getonepage(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}
response = requests.get(url,headers=headers)
if response.status_code == 200:
return response.text
return None
def fenxi(html):
zhengze = re.compile('img src="(.*?)"',re.S)
pic_url = re.findall(zhengze,html)
pic_url_rea = pic_url[3]
picgit = requests.get(pic_url_rea)
print(pic_url_rea)
nameurl = re.compile('_.*')
picname = re.findall(nameurl,pic_url_rea)
print(picname)
with open(f'./azhu/{picname}.jpg','wb') as f:
f.write(picgit.content)
f.close()
print(f'已完成{picname}的下载')
def main():
urls = ['https://www.umtuba.com/siwameitui/56995_1.html']
for i in range(1,68):
urls.append(f'https://www.umtuba.com/siwameitui/56995_{i}.html')
# print(urls)
for url in urls:
html = getonepage(url)
time.sleep(1)
fenxi(html)
print('图片下载已全部完成!')
main()
以上是所有代码,我觉得和我照着书敲的很多地方不同(虽然大部分基本也都是照着敲),而且代码好像很繁杂,希望大佬给点优化意见
已经很工整了,用正则就是这样,要代码简单用bs4或者lxml
|
|