python之爬蟲-必應壁紙
import re
import requests
"""
@author RansySun
@create 2019-07-19-20:26
"""
"""
https://bing.ioliu.cn/?p=1"
https://bing.ioliu.cn/?p=2"
https://bing.ioliu.cn/?p=3"
"""
count = 1
for i in range(50):
url = f"https://bing.ioliu.cn/?p={i}"
reponse = requests.get(url)
data = reponse.text
# 獲取必應壁紙圖片的鏈接
result_list = re.findall('src="(.*?)"', data)
g = 0
for result in result_list:
# 處理其他鏈接
if g < 4:
g += 1
continue
# 判斷是否是圖片的鏈接
if result.startswith("https"):
# 請求圖片地址
img_response = requests.get(result)
img_data = img_response.content
# 獲取圖片名
img_name = result.split("/")[-1].split("_")[0]+".jpg"
img_star_name = str(count) + "_" + img_name
# 寫入圖片
with open(img_star_name, "wb") as fw:
fw.write(img_data)
print("爬取成功:", img_star_name)
fw.flush()
count += 1
print(result)
結果: