python bs4爬取彼岸网任意页面的壁纸(以爬取1080p)为例
一:原创,过程可能较重复,但实现逻辑简单:
整个爬取逻辑:从一个页面跳转到另一个页面再获取(高清无损)壁纸的具体网址,然后下载就可。
二:用到的模块:requests,beautifulsoup模块*
***三:具体实现代码(完整可直接使用)***:
import re
import requests
import time#####
from bs4 import BeautifulSoup
from lxml import etree
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"
}
url="http://www.netbian.com/1920x1080/index_3.htm"
main_page=requests.get(url=url,headers=headers)
main_page.encoding=main_page.apparent_encoding
soup=BeautifulSoup(main_page.text,"lxml")
li_list=soup.select(".list li>a")
for li in li_list:
li_herf=li["href"]
the_real="http://www.netbian.com/"+li_herf
response=requests.get(url=the_real,headers=headers)
response.encoding=response.apparent_encoding
real_soup=BeautifulSoup(response.text,"lxml")
img=real_soup.select(".pic img")[0]["src"]#获得图片网址
img_resp=requests.get(url=img,headers=headers)#request解析
img_name=img.split("/")[-1]#根据具体而修改
# print(img_name)
# print(img)
with open("bian/"+img_name,mode="wb")as f:
f.write(img_resp.content)#不需要修改
print("over",img_name)
time.sleep(1)#防被服务器检测禁止ip访问
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?