hxmhh 2023-09-17 19:30 采纳率: 0%
浏览 11

python抓取国外网站时出现403错误


用python来抓取国外网站时出现403错误

代码如下

import requests
from bs4 import BeautifulSoup

url = 'https://7net.omni7.jp/search/?keyword=%E3%83%8F%E3%82%A4%E3%82%AD%E3%83%A5%E3%83%BC%EF%BC%81%EF%BC%81%E3%82%B9%E3%82%AF%E3%82%A8%E3%82%A2%E7%BC%B6%E3%83%90%E3%83%83%E3%82%B8'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8',
    'Cache-Control': 'max-age=0',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31',
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    print('页面请求成功!')
    print(response.status_code)
    print(response.url)
    soup = BeautifulSoup(response.content, 'html.parser')
    print(soup)
elif response.status_code == 302:
    redirect_url = response.headers.get('Location')
    print('页面已重定向到新的URL地址:', redirect_url)
else:
    print('页面请求失败,状态码为:', response.status_code)

如果在header中加入cookie时,可以正常抓取到数据,但是不能够每次抓数据的时候我都用浏览器获取到cookie,再写入到headers里面来抓取吧,这样就失去了爬虫的意义。

我的想法是通过访问某个页面来获取到session,再带session来访问我要抓取的页面来抓取数据。

我也尝试过下面的代码,但是还是出现403错误,

import time

import requests
from bs4 import BeautifulSoup

# 创建一个会话
session = requests.Session()

url_top = 'https://7net.omni7.jp/'
url = 'https://7net.omni7.jp/search/?keyword=%E3%83%8F%E3%82%A4%E3%82%AD%E3%83%A5%E3%83%BC%EF%BC%81%EF%BC%81%E3%82%B9%E3%82%AF%E3%82%A8%E3%82%A2%E7%BC%B6%E3%83%90%E3%83%83%E3%82%B8'
headers = {   
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8',
    'Cache-Control': 'max-age=0',
    'Sec-Ch-Ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31',
}
response = session.get(url_top, headers=headers)
if response.status_code == 200:
    print('页面请求成功!')
    print(response.status_code)
    print(response.url)
    time.sleep(3)

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        print('页面请求成功!')
        print(response.status_code)
        print(response.url)
        soup = BeautifulSoup(response.content, 'html.parser')
        print(soup)
    elif response.status_code == 302:
        redirect_url = response.headers.get('Location')
        print('页面已重定向到新的URL地址:', redirect_url)
    else:
        print('页面请求失败,状态码为:', response.status_code)

elif response.status_code == 302:
    redirect_url = response.headers.get('Location')
    print('页面已重定向到新的URL地址:', redirect_url)
else:
    print('页面请求失败,状态码为:', response.status_code)

运行结果为

页面请求成功!
200
https://7net.omni7.jp/top
页面请求失败,状态码为: 403

Process finished with exit code 0

希望得到大家的帮助,谢谢

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-09-17 19:59
    关注

    引用 皆我百晓生 小程序回复内容作答:
    403错误表示服务器拒绝了你的请求。在这种情况下,服务器认为你的请求是非法的或被禁止的,并且不提供所需的数据。

    有几种原因可能导致403错误:

    1. 缺少必要的认证信息:某些网站要求进行身份验证或使用API密钥来访问其内容。如果你没有提供正确的认证信息,服务器可能会拒绝你的请求。

    2. IP被封禁:有些网站可能会封禁特定的IP地址,如果你的IP地址被封禁,你将无法访问该网站。

    3. 爬取频率过高:如果你的爬虫程序过于频繁地请求网站,服务器可能会将你的请求视为DDoS攻击或资源滥用。

    为了解决这个问题,你可以尝试以下几个步骤:

    1. 检查是否需要提供认证信息:查看网站的API文档或用户协议,确定是否需要提供认证信息,如API密钥、用户名和密码等。如果需要,你可以将认证信息添加到请求的header中,以便正确访问网站。

    2. 降低爬取频率:如果你的爬虫请求过于频繁,可以尝试减少请求的频率,增加请求之间的间隔时间,以避免被服务器封禁。

    3. 使用代理IP:如果你的IP地址已经被封禁,你可以尝试使用代理IP来改变你的访问来源。代理IP可以帮助你隐藏真实的IP地址,从而绕过封禁。

    4. 联系网站管理员:如果以上方法都无法解决问题,你可以尝试联系网站的管理员或技术支持团队,了解为什么你的请求被拒绝,并寻求他们的帮助。

    注意:在进行Web爬虫时,请确保你的行为符合网站的使用条款和法律法规,并尊重网站所有者的权益。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月17日