Python爬取小姐姐美照!

2022-06-08 05:57:09 字數 1755 閱讀 9058

今天教大家用如何用爬蟲爬取可愛小姐姐的美照

第三步:分析目標**的html原始碼及網頁規則

通過分析我發現,該網頁每個人物的每個都是

from bs4 import beautifulsoup #引用beautifulsoup庫

import requests #引用requests

import os #os

root = 'd://img//' #配置儲存路徑,我配置的是自己電腦中的d:/img資料夾

for page in range(1,1000): #配置爬取頁碼,我這邊配置的是1000個人的

for p in range(1,20): #配置爬取每個人多少張的引數,我這邊配置的是每個人20張

url = ''+str(page)+'_'+str(p)+'.html'

r = requests.get(url) #使用requests中的get方法獲取整個網頁

r.encoding='utf-8' #設定網頁所使用的編碼方式,錯誤的編碼方式會導致亂碼

demo = r.text #將爬取後的物件通過text方法提取出所有的html

soup = beautifulsoup(demo, "html.parser")#使用beautifulsoup庫進行整合,第二個引數使用lxml一樣的,lxml相容性好較好,速度較快

text = soup.find_all('img',class_ = 'pic-large')#選取整合後我們需要的部分內容,選取後的資料為list陣列

for img in text:

imagr_url = img.get('data-original') #取出img標籤中data-original中的值

file_name = root + imagr_url.split('/')[-1] #取出位址中檔案及副檔名與本地儲存路徑進行拼接

try:

if not os.path.exists(root): #判斷資料夾是否存在,不存在則建立資料夾

os.mkdir(root)

if not os.path.exists(file_name): #判斷檔案是否存在,存在則進行提示

s = requests.get(imagr_url) #通過requests.get方式獲取檔案

# 使用with語句可以不用自己手動關閉已經開啟的檔案流

with open(file_name, "wb") as f: # 開始寫檔案,wb代表寫二進位制檔案

f.write(s.content)

print("爬取完成")

else:

print("檔案已存在")

except exception as e:

print("爬取失敗:" + str(e))

看完文章如果大家覺得有幫助,記得一鍵三連哦,非常感謝,在這裡還是要推薦下我自己建的python學習群:609616831,群裡都是學python的,如果你想學或者正在學習python ,歡迎你加入,大家都是軟體開發黨,不定期分享乾貨(只有python軟體開發相關的),包括我自己整理的乙份2020最新的python高階資料和零基礎教學,歡迎高階中和對python感興趣的小夥伴加入!

Python 將小姐姐畫在Excel上

準備 1.一張小姐姐的 2.python 環境 3.需要匯入的庫 pil,xlsxwriter 原理 1.讀取小姐姐的 轉化為rgb 格式 2.將顏色填充到對應的 上 很簡單如下 from pil import image import xlsxwriter 顏色轉換函式 defcolor valu...

Scrapy爬取兩萬張小姐姐並按資料夾儲存

scrapy startproject tupian coding utf 8 define here the models for your scraped items see documentation in import scrapy class tupianitem scrapy.item ...

Python 之將小姐姐畫在Excel上

from pil import image import xlsxwriter 顏色轉換函式 defcolor value digit list map str range 10 list abcdef if isinstance value,tuple string for i in value ...