使用Python爬取小姐姐圖片
首先上網站鏈接 唯美女生
爬取圖片主要分為一下幾步:
1.打開一個你喜歡的小姐姐的網站
E.g xiaojiejie web
2.下載并安裝python環境
python 官網
菜鳥教程-python環境搭建教程
3.開始編碼
python 全部代碼如下
# requests 請求 需要提前在Terminal中安裝 pip install requests
import os
import time
import requests
# re正則
import re
# 改變自己身份
headers = {
'User-Agent': 'asbasdf'
}
# 請求網頁
print("請輸入你要爬取網站的鏈接")
httpurl = input()
response = requests.get(httpurl,headers = headers)
print(response.request.headers)
print(response.text)
html = response.text
# 決議網頁
# view-source:https://www.vmgirls.com/15159.html
# 鏈接前加view-source查看網頁源代碼
dir_name = re.findall('<h1 class="post-title h1">(.*?)</h1>',html)[-1]
if not os.path.exists(dir_name):
os.mkdir(dir_name)
# 正則查找
urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">',html)
print(urls)
# 保存圖片
for url in urls:
time.sleep(1)
# 圖片名字
name = url.split('/')[-1]
response = requests.get("https:"+url,headers = headers)
print(name+"正在下載")
with open(dir_name+'/'+name,'wb') as f:
f.write(response.content)
print('下載完畢')
4.運行并下載


教程完畢
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/246175.html
標籤:python
上一篇:海龜畫圖全解--值得你一看!
