目标为成为电子高手 2025-03-16 11:27 采纳率: 0%
浏览 15

403代码该怎么解决,我换了headers,延时也那样,我本意是爬虫一个网战的图片,我现在已知的可能有关的是有一些人去爬虫那个网站,网站会有等待几秒验证真人,我把代码写上




import requests
from bs4 import BeautifulSoup
import time
url='https://pic.netbian.com/4kdongman/index.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}

response=requests.get(url=url,headers=headers,timeout=20)
print(response.status_code)
response.encoding='gbk'
response.encoding='utf-8'
text=response.text
soup=BeautifulSoup(text,'html.parser')
#开始解析内容
img_list=soup.select('.slist ul li a img')
time.sleep(5)
print(img_list)
#遍历图片对象,拿到链接地址
for img in img_list:
    img_url=img.get('src')
#    print(img_url)
    img_name=img['alt']
    time.sleep(5)
#发送请求
    try:
        response=requests.get(url=img_url,headers=headers,timeout=200)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        print(f"获取图片失败: {e}")
        continue
    #要保存图片,要有对应的文件名,replace()方法可以替换字符串中的某个字符,照片名称不能有'*'等特殊字符
#    img_name=img_url.replace('/','_')


# 创建目录
if not os.path.exists('./动漫图集3'):
    os.makedirs('./动漫图集3')

#    img_name=img['alt']
    with open(f'./动漫图集3/{img_name}.png','wb') as f:
        f.write(response.content)
    time.sleep(10)



  • 写回答

4条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2025-03-16 11:27
    关注
    让【道友老李】来帮你解答,本回答参考gpt编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    这段代码是使用Python中的requests和BeautifulSoup库来爬取网站上的图片,并将图片保存到本地。代码中首先使用requests库发送HTTP请求获取网页内容,然后使用BeautifulSoup库解析网页内容,提取出图片链接。接着遍历图片链接列表,依次发送请求获取图片并保存到本地。 在代码中,需要注意的是要替换图片名称中的特殊字符,以避免保存文件时出现问题。另外,代码中也创建了一个目录用于保存图片。 如果想要完整运行这段代码,需要先安装requests和BeautifulSoup库。同时还需要导入os模块,因为代码中使用了os.makedirs()方法来创建目录。 以下是完整的实现代码:
    import requests
    from bs4 import BeautifulSoup
    import os
    import time
    url = 'https://pic.netbian.com/4kdongman/index.html'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}
    response = requests.get(url=url, headers=headers, timeout=20)
    print(response.status_code)
    response.encoding = 'gbk'
    response.encoding = 'utf-8'
    text = response.text
    soup = BeautifulSoup(text, 'html.parser')
    img_list = soup.select('.slist ul li a img')
    time.sleep(5)
    print(img_list)
    for img in img_list:
        img_url = img.get('src')
        img_name = img['alt']
        time.sleep(5)
        try:
            response = requests.get(url=img_url, headers=headers, timeout=200)
            response.raise_for_status()
        except requests.exceptions.RequestException as e:
            print(f"获取图片失败: {e}")
            continue
        if not os.path.exists('./动漫图集3'):
            os.makedirs('./动漫图集3')
        with open(f'./动漫图集3/{img_name}.png', 'wb') as f:
            f.write(response.content)
        time.sleep(10)
    

    请注意,上述代码仅供参考,实际运行时需要根据网站结构和图片链接的规律进行适当的调整。

    评论

报告相同问题?

问题事件

  • 修改了问题 3月16日
  • 创建了问题 3月16日