当前位置:网站首页>Python爬蟲實戰詳解:爬取圖片之家
Python爬蟲實戰詳解:爬取圖片之家
2020-11-06 01:17:00 【itread01】
前言
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理
如何使用python去實現一個爬蟲?
- 模擬瀏覽器
請求並獲取網站資料
在原始資料中提取我們想要的資料 資料篩選
將篩選完成的資料做儲存
完成一個爬蟲需要哪些工具
- Python3.6
- pycharm 專業版
目標網站
圖片之家
https://www.tupianzj.com/
爬蟲程式碼
匯入工具
python 自帶的標準庫
import ssl
系統庫 自動建立儲存資料夾
import os
下載包
import urllib.request
網路庫 第三方包
import requests
網頁選擇器
from bs4 import BeautifulSoup
預設請求https網站不需要證書認證
ssl._create_default_https_context = ssl._create_unverified_context
模擬瀏覽器
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
}
自動建立資料夾
if not os.path.exists('./插畫素材/'):
os.mkdir('./插畫素材/')
else:
pass
請求操作
url = 'https://www.tupianzj.com/meinv/mm/meizitu/' html = requests.get(url, headers=headers).text
對頁面原始資料做資料提取
soup = BeautifulSoup(html, 'lxml')
images_data = soup.find('ul', class_='d1 ico3').find_all_next('li')
for image in images_data:
image_url = image.find_all('img')
for _ in image_url:
print(_['src'], _['alt'])
下載
try:
urllib.request.urlretrieve(_['src'], './插畫素材/' + _['alt'] + '.jpg')
except:
pass
效果圖
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://www.itread01.com/content/1604499424.html
边栏推荐
- DRF JWT authentication module and self customization
- Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
- python过滤敏感词记录
- Kitty中的动态线程池支持Nacos,Apollo多配置中心了
- Analysis of ThreadLocal principle
- [performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
- 读取、创建和运行多个文件的3个Python技巧
- 向北京集结!OpenI/O 2020启智开发者大会进入倒计时
- Clean架构能够解决哪些问题? - jbogard
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
猜你喜欢
随机推荐
写一个通用的幂等组件,我觉得很有必要
【C/C++ 2】Clion配置与运行C语言
面经手册 · 第12篇《面试官,ThreadLocal 你要这么问,我就挂了!》
7.3.1 file upload and zero XML registration interceptor
技術總監,送給剛畢業的程式設計師們一句話——做好小事,才能成就大事
【Flutter 實戰】pubspec.yaml 配置檔案詳解
Menu permission control configuration of hub plug-in for azure Devops extension
6.9.1 flashmapmanager initialization (flashmapmanager redirection Management) - SSM in depth analysis and project practice
ETCD核心機制解析
Chainlink将美国选举结果带入区块链 - Everipedia
5.4 静态资源映射 -《SSM深入解析与项目实战》
How to demote a domain controller in Windows Server 2012 and later
tensorflow之tf.tile\tf.slice等函数的基本用法解读
微服務 - 如何解決鏈路追蹤問題
利用 AWS SageMaker BlazingText 对不均衡文本进行多分类
7.2.1 cache configuration of static resources
快快使用ModelArts,零基础小白也能玩转AI!
Elasticsearch database | elasticsearch-7.5.0 application construction
被老程式設計師壓榨怎麼辦?我不想辭職
Top 10 best big data analysis tools in 2020




