# -*- coding: utf-8 -*-
# @Time : 2020/9/15 21:26
# @Author : aqiong
# @Site :
# @File : 正则爬虫练习2.py
# @Software: PyCharm
import requests
import re
import os
#糗图
if __name__ == '__main__':
if not os.path.exists('./img'):
os.makedirs('./img')
count=1
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
for page in range(1,4):#1-4页的内容
url = 'https://www.qiushibaike.com/imgrank/page/'+str(page)
#url = 'https: // www.qiushibaike.com / imgrank / page / 3 /
page_text = requests.get(url=url,headers=headers).text
ex = '<div class="thumb">.*?<img src="(.*?)".*?alt=.*?</div>'
img_src_list = re.findall(ex,page_text,re.S)
#print(img_src_list)
for img_src in img_src_list:
img_byte = requests.get('https:' + img_src, headers).content
fileName = img_src.split('/')[-1]
with open('./img/'+fileName,'wb') as fp:
fp.write(img_byte)
print('下载成功!'+str(count))
count+=1