python爬取换页_python分页爬取网站的图片

python分页爬取网站的图片

import requests

import re

import os

import time

"""获取主网页"""

web_page = 'https://www.vmgirls.com/'

headers = {

'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'

}

urls_response = requests.get(web_page,headers=headers)

urls_html = urls_response.text

"""解析主主网页获取下一层网页"""

all_urls = re.findall('https://.*?/\d*.html',urls_html)

urls = list(set(all_urls))

# print(urls)

"""下载下一页的网页图片"""

num_list = []

for url in urls:

url_resopnse = requests.get(url,headers=headers)

html=url_resopnse.text

dir_name = re.findall('

(.*?)

',html)[-1]

wget_urls = re.findall('https:.*?.jpeg',html)

print("\033[32;1m %s upload %s pictures\033[0m" %(dir_name,len(wget_urls)))

num = len(wget_urls)

num_list.append(num)

for wget_url in wget_urls:

time.sleep(1)

file_name = wget_url.split('/')[-1]

print(file_name)

dir_name = re.findall('

(.*?)

',html)[-1]

if not os.path.exists(dir_name):

os.mkdir(dir_name)

response = requests.get(wget_url,headers=headers)

with open(dir_name + '/' + file_name,'wb') as f:

f.write(response.content)

sum = 0

for i in num_list:

sum += i

print("\033[31;1mThere are %s pictures that need to be crawled\033[0m" % (sum))

©著作权归作者所有:来自51CTO博客作者Leo_zhjl的原创作品,如需转载,请注明出处,否则将追究法律责任

好知识,才能预见未来

赞赏

0人进行了赞赏支持

noavatar_middle.gif

Leo_zhjl

36篇文章,16W+人气,3粉丝

没有什么是永恒的,但没有什么是永恒的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值