

1.1 网址
# 谷歌浏览器http://www.netbian.com/weimei/
1.2 查看网页源代码
# 1 直接获取会遇到防火墙,添加verify=False去掉安全认证# 2 response.encoding = 'gbk'需要考虑原页面的代码# 查看代码中charest然后设定格式*- 网址特点需要二进宫,进入该网址然后将结果输出
爬取数据
2.1 导入包
import requestsfrom bs4 import BeautifulSoup
2.2 获取数据

url= 'http://www.netbian.com/weimei/'headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"}# 获取主页面的页面的源代码并处理文字response = requests.get(url,headers=headers,verify=False)response.encoding = 'gbk'print(response.text)
2.3 解析数据
# 主页面的源代码交给bs4domain = 'http://www.netbian.com/'main_page = BeautifulSoup(response.text,"html.parser")a_list = main_page.find("div",class_="list").find_all("a") # 范围第一次缩小a_list
*- 提取到的内容:href="/desk/23552.htm"*- 实际网址:http://www.netbian.com/desk/23552.htm*- 解决思路domain = 'http://www.netbian.com/'href = domain + b 获取第二次爬取的链接进入子链接后查看网页源代码——>通过获取信息下载该图片
2.4 下载数据
# 获取子页面的链接并进入子页面for a in a_list:b = a.get("href").strip("/")if b.startswith('desk'): # 判断开头href = domain + b # 直接通过get获取相关图片的链接child_response = requests.get(href,headers=headers,verify=False)child_response.encoding = 'gbk'child_page_text = child_response.text # 获取子页面的内容child_page = BeautifulSoup(child_page_text,"html.parser")div = child_page.find("div",class_="endpage") # 范围第二次缩小img = div.find("img") # 发现整个链接前一个src = img.get("src") # 需要什么属性就get什么,获取高清图片链接# 下载图片img_response = requests.get(src)# 图片内容下载到文件img_name = src.split("/")[-1] # 定义名字with open("img2/"+img_name,mode="wb") as f:f.write(img_response.content)print("over!",img_name)print('all over !!!')
Python源代码:
import requests
from bs4 import BeautifulSoup
url= 'http://www.netbian.com/weimei/'
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
}
# 获取主页面的页面的源代码并处理文字
response = requests.get(url,headers=headers,verify=False)
response.encoding = 'gbk'
print(response.text)
# 主页面的源代码交给bs4
domain = 'http://www.netbian.com/'
main_page = BeautifulSoup(response.text,"html.parser")
a_list = main_page.find("div",class_="list").find_all("a") # 范围第一次缩小
a_list
# 获取子页面的链接并进入子页面
for a in a_list:
b = a.get("href").strip("/")
if b.startswith('desk'): # 判断开头
href = domain + b # 直接通过get获取相关图片的链接
child_response = requests.get(href, headers=headers, verify=False)
child_response.encoding = 'gbk'
child_page_text = child_response.text # 获取子页面的内容
child_page = BeautifulSoup(child_page_text, "html.parser")
div = child_page.find("div", class_="endpage") # 范围第二次缩小
img = div.find("img") # 发现整个链接前一个
src = img.get("src") # 需要什么属性就get什么,获取高清图片链接
# 下载图片
img_response = requests.get(src)
# 图片内容下载到文件
img_name = src.split("/")[-1] # 定义名字
with open("img2/" + img_name, mode="wb") as f:
f.write(img_response.content)
print("over!", img_name)
print('all over !!!')

下载图片之前要建立文件夹
下载图片之前要建立文件夹
下载图片之前要建立文件夹

本文介绍了如何使用Python爬虫技术,通过谷歌浏览器抓取并解析www.netbian.com/weimei/页面的链接,深入子页面下载高清图片,涉及HTTP请求、源代码解析和文件操作技巧。

1万+

被折叠的 条评论
为什么被折叠?



