import urllib.request
import re
import os
def download_image(image_url):
dirpath = './qiubai'
# 获取文件名
filename = os.path.basename(image_url)
# 拼接文件全路径
filepath = os.path.join(dirpath, filename)
# 下载图片
urllib.request.urlretrieve(image_url, filepath)
print(filepath + '下载完毕')
def handle_content(request):
# 拿到页面
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
# 处理html,通过正则表达式拿到所有的图片链接,并且将图片下载到本地
pattern = re.compile(r'<div class="thumb">.*?<img src=(.*?) alt=.*?>.*?</div>', re.S)
# 获取所有的图片的链接
src_list = pattern.findall(html)
# 遍历这个列表
for src in src_list:
# 取出两边的双引号
src = src.strip('"')
# 拼接完整的图片url
image_url = 'https:' + src
# 下载这个图片即可
download_image(image_url)
# 构建请求对象并且返回
def handle_url(url, page):
url = url + str(page)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'
}
request = urllib.request.Request(url=url, headers=headers)
return request
def main():
url = 'https://www.qiushibaike.com/pic/page/'
start_page = int(input('请输入抓取的起始页码:'))
end_page = int(input('请输入抓取的结束页码:'))
print('开始下载...........')
for page in range(start_page, end_page + 1):
# 拼接url,生成一个request
request = handle_url(url, page)
# 获取内容,处理内容
handle_content(request)
print('全部下载完毕')
if __name__ == '__main__':
main()
python爬虫爬取糗事百科图片
最新推荐文章于 2021-12-26 03:04:01 发布