python爬取網頁圖片
阿新 • • 發佈:2020-11-27
1.爬取一個頁面的圖片
# encoding=gbk
import re
import requests
url='http://www.netbian.com/'
data=requests.get(url).text
jpglist=re.findall('<img src="(.*?)" ',data,re.S)
n=1
for each in jpglist:
print(each)
try:
pic=requests.get(each,timeout=10)
except:
print('下載失敗')
continue
string='D:\\img\\1\\'+str(n)+'.jpg'
fp=open(string,'wb')
fp.write(pic.content)
fp.close()
n+=1
- 爬取網站的全部圖片
在這裡插入程式碼片
以下是實現中遇到的問題以及解決方案總結
1. ModuleNotFoundError: No module named ‘requests’
未安裝該模組,安裝一下即可;
在python安裝目錄輸入cmd,再輸入以下程式碼:
python -m pip install request
2. ModuleNotFoundError: No module named ‘bs4’
python -m pip install bs4
3. bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml.
python -m pip install lxml
這裡看見有人安裝失敗
原因是電腦安裝了Anaconda3(python3.7),lxml庫是4.2.5,在Pycharm(社群,2018.3)版中老是不支援lxml。
解決方案:先用pip uninstall lxml解除安裝,再用pip install lxml安裝,然後成功了