实战爬取某网站图片-Python

直接上代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import requests
import sys
import os
 
#获取图集id
try:
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55"}
    html = requests.get('https://www.tujigu.com/', headers=headers)
    html.encoding = 'utf-8-sig'
    soup = BeautifulSoup(html.text,"html.parser").find_all('p',class_='biaoti')
    dict = {}
    for i in soup:
        id_url = i.find('a')['href']
        id = id_url[25:len(id_url)-1]
        name_str = i.find('a').contents[0]
        name = name_str.replace(" ", "")
        dict.update({id:name})
except:
    print("获取id和name失败")
    sys.exit()
 
 
for id,name in dict.items():
    #创建目录
    path= r"C:\Users\ATRAY\Documents\Downloads\{}".format(name)
    folder = os.path.exists(path)
    if not folder:                   
        os.makedirs(path)            
    else:
        print ("创建目录失败")
    #下载图片
    num = 1
    while True:
        picture_url="https://tjg.gzhuibei.com/a/1/{}/{}.jpg".format(id,num)
        picture=requests.get(picture_url,headers=headers)
        if picture.status_code == 200:
            with open(path + "\{}.jpg".format(num),"wb") as code:
                code.write(picture.content)
            num = num + 1
        else:
            print("以下载{}写真{}张".format(name,num - 1))
            break
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇