前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
Python爬蟲、資料分析、網站開發等案例教程視頻免費在線觀看
https://space.bilibili.com/523606542
前文內容
Python爬蟲新手入門教學(一):爬取豆瓣電影排行資訊
Python爬蟲新手入門教學(二):爬取小說
Python爬蟲新手入門教學(三):爬取鏈家二手房資料
Python爬蟲新手入門教學(四):爬取前程無憂招聘資訊
Python爬蟲新手入門教學(五):爬取B站視頻彈幕
Python爬蟲新手入門教學(六):制作詞云圖
Python爬蟲新手入門教學(七):爬取騰訊視頻彈幕
Python爬蟲新手入門教學(八):爬取論壇文章保存成PDF
基本開發環境
- Python 3.6
- Pycharm
- wkhtmltopdf
相關模塊的使用
- re
- requests
- concurrent.futures
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
一、明確需求
現在聊天誰還不發幾個表情包?聊天時,表情包是我們重要的工具,更是拉進小伙伴們距離的好幫手,當聊天陷入尷尬境地時,隨手一張表情包,讓尷尬化為無形
本篇文章就用python批量爬取表情包圖片,留以備用
二、網頁資料分析
如圖所示斗圖網上面的圖片資料都包含在 a 標簽當中,可以嘗試直接請求這個網頁,查看response 回傳的資料當中是否也含有 圖片地址,
import requests
def get_response(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
return response
def main(html_url):
response = get_response(html_url)
print(response.text)
if __name__ == '__main__':
url = 'https://www.doutula.com/photo/list/'
main(url)
在輸出結果中 ctrl + F 進行搜索,
這里有一個點想要注意一下,我用python請求網頁所給我們回傳的結果當中,包含圖片url地址是:
data-original="圖片url"
data-backup="圖片url"
如果想要提取url地址的話,可以用parsel 決議庫,或者 re 正則運算式,之前都是使用的parsel,本篇文章就用 正則運算式吧,
urls = re.findall('data-original="(.*?)"', response.text)
單頁爬取完整代碼
import requests
import re
def get_response(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
return response
def save(image_url, image_name):
image_content = get_response(image_url).content
filename = 'images\\' + image_name
with open(filename, mode='wb') as f:
f.write(image_content)
print(image_name)
def main(html_url):
response = get_response(html_url)
urls = re.findall('data-original="(.*?)"', response.text)
for link in urls:
image_name = link.split('/')[-1]
save(link, image_name)
if __name__ == '__main__':
url = 'https://www.doutula.com/photo/list/'
main(url)
多執行緒爬取全站圖片(如果你的記憶體夠大)
3631頁的資料,什么表情都有,嘿嘿嘿
import requests
import re
import concurrent.futures
def get_response(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
return response
def save(image_url, image_name):
image_content = get_response(image_url).content
filename = 'images\\' + image_name
with open(filename, mode='wb') as f:
f.write(image_content)
print(image_name)
def main(html_url):
response = get_response(html_url)
urls = re.findall('data-original="(.*?)"', response.text)
for link in urls:
image_name = link.split('/')[-1]
save(link, image_name)
if __name__ == '__main__':
# ThreadPoolExecutor 執行緒池的物件
# max_workers 最大任務數
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
for page in range(1, 3632):
url = f'https://www.doutula.com/photo/list/?page={page}'
# submit 往執行緒池里面添加任務
executor.submit(main, url)
executor.shutdown()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253829.html
標籤:Python

