关于python爬虫爬取网页图片的实例

#实例1,爬取http://www.facets.la/壁纸网站,目前实现爬取某一页,可在此基础上修改
import re
import requests
import time
from bs4 import BeautifulSoup
#爬虫一
url="http://www.facets.la/"
headers={
"User-Agent":"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36"
}
main_page_respose=requests.get(url=url,headers=headers)
main_page_respose.encoding=main_page_respose.apparent_encoding
#print(main_page_respose.text)
xipage=BeautifulSoup(main_page_respose.text,"html.parser")
a_list=xipage.find_all("div",class_="thumb-image")
for a in a_list:
   a_shuxi=a.find("a")
   a_img=a_shuxi.find("img")
   #print(a_img)
   a_reals=a_img.get("src")
   #print(a_reals)
   #下载
   img_response=requests.get(a_reals,headers=headers)
   img_name=a_reals.split("/")[-1]#根据具体而修改
   with open("download/"+img_name,mode="wb")as f:
        f.write(img_response.content)#不需要修改
        print("over",img_name)
        time.sleep(1)#防被服务器检测禁止ip访问
print("over")

感谢哔哩哔哩up主的爬虫教程
附上up主主页链接
python爬虫教程

posted @   昊月光华  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示