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

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

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

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

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

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

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

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

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

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

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

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

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

【以下回答由 GPT 生成】

如何解决使用Python抓取国外网站时出现403错误?

当遇到403错误时,说明你的请求被服务器拒绝了。这通常是由于该网站的访问限制机制导致的。以下是解决该问题的几种常见方法:

  1. 检查请求头信息:
  2. 有些网站会要求特定的请求头信息才允许访问。你可以使用浏览器开发者工具来查看正常访问该网站时的请求头信息,并将其复制到你的代码中。确保请求头中包含所需的信息,如User-AgentReferer等。

示例代码:

```python headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT ; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36', 'Referer': 'https://www.example.com' }

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

  1. 修改User-Agent:
  2. 有些网站使用反爬虫技术,会检测请求头中的User-Agent信息。可以尝试将User-Agent信息修改为与正常浏览器的相似,以模拟真实的浏览器访问。

示例代码:

```python headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT ; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36' }

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

  1. 使用代理服务器:
  2. 有些网站会根据IP地址进行限制。你可以尝试使用代理服务器来请求该网站,将请求经过代理服务器发送,改变请求的来源IP地址,绕过一些限制。

示例代码:

```python proxies = { 'http': 'http://:', 'https': 'https://:' }

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

需要根据代理服务器的配置填写<proxy_ip><proxy_port>

  1. 联系网站管理员:
  2. 如果以上方法都无效,可能是该网站对爬虫做了更严格的限制。你可以考虑联系网站管理员,获得访问权限或了解他们的数据提供方式。

希望以上解决方法能够帮助你成功抓取该网站的内容。如果你有任何进一步的疑问,请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^