Python爬蟲 | 爬取高質量小姐姐照片
- 1、資料來源分析
- 2、獲取author_id_list和img_id
- 3、代碼實作
- 3.1、制作detial
- 3.2、制作detial_list
- 3.3、資料保存
- 3.4、批量獲取
- 4、完整代碼
- 宣告
1、資料來源分析
??在網頁HTML源代碼里,我們找到了每一張照片的地址為
https://photo.tuchong.com/5489136/f/360962642.jpg
??決議如下
https://photo.tuchong.com/author_id_list/f/img_id.jpg
??其中author_id_list和img_id都是我們需要自己獲取的
2、獲取author_id_list和img_id
??打開網站圖蟲網首頁,經過分析,發現資料請求是動態加載的來源是下面的請求

??回傳的資料中包含author_id_list和img_id,一個author_id_list對應好幾個img_id,因此在獲取資料的時候對每一個author_id_list下的圖片進行單獨保存,建立獨立的檔案夾
3、代碼實作
3.1、制作detial
??將每一個author_id_list和img_id保存到一個detial中,利用鍵值對的形式,author_id_list存放author_id_list,img_id存放img_id

3.2、制作detial_list
??將每一個detial存放在detial_list,那么第一頁的所需資料就準備好了

3.3、資料保存
??對每一個author_id建立應的檔案夾

3.4、批量獲取
??前面的請求引數有page和count,表示請求頁數和每一頁的資料量,將page放在回圈中就行了

4、完整代碼
import requests
import os
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.44",
}
for i in range(1, 11):
url = f'https://tuchong.com/rest/tags/%E7%BE%8E%E5%A5%B3/posts?page={i}&count=20&order=weekly&before_timestamp='
response = requests.get(url=url, headers=headers)
page = response.json()["postList"]
# 制作detial_list
detial_list = []
for item in page:
author_id_list = ""
img_id = []
detial = {
"author_id_list": author_id_list,
"img_id": img_id
}
author_id_list = (item["author_id"])
for it in item["images"]:
img_id.append(it["img_id"])
detial["author_id_list"] = author_id_list
detial["img_id"] = img_id
detial_list.append(detial)
for item in detial_list:
# 新建檔案夾
author_id_list = item["author_id_list"]
if not os.path.exists(f'img/{author_id_list}'):
os.makedirs(f"img/{author_id_list}")
# 保存資料
for it in item["img_id"]:
with open(f'img/{author_id_list}/{it}.jpg', mode="wb") as fp:
page = requests.get(url=f"https://photo.tuchong.com/{author_id_list}/f/{it}.jpg", headers=headers)
data = page.content
fp.write(data)
# 列印提示資訊
print(f"第{i}頁完成...")
??畢竟圖蟲網提供高質量的圖片,而且都是著作權所有的,也就只是用于學習交流,不要太過分,爬取10頁資料

??源代碼地址:gitee
宣告
本文僅限于做技術交流學習,請勿用作任何非法商用!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316652.html
標籤:python
上一篇:2021年10月世界編程語言排行
