【问题标题】:Python3 - urllib.error.HTTPError: HTTP Error 403: ForbiddenPython3 - urllib.error.HTTPError:HTTP 错误 403:禁止
【发布时间】:2015-01-19 21:29:09
【问题描述】:

我正在尝试为我的域列表获取 Google PageRank,但我最终收到此错误:

Python3: raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

关于我的问题,我已经尝试了一些现有的解决方案,但没有一个解决了我的问题。这是我的代码:

#  Script for getting Google Page Rank of page
#  Google Toolbar 3.0.x/4.0.x Pagerank Checksum Algorithm
#
#  original from http://pagerank.gamesaga.net/
#  this version was adapted from http://www.djangosnippets.org/snippets/221/
#  by Corey Goldberg - 2010
#
#  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php


from __future__ import print_function, division
import sys
import urllib.request as _urlib1  # py3 
import urllib.parse as _urlib2  # py 3




def get_pagerank(url):
    hsh = check_hash(hash_url(url))
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
    gurl = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank&ch=%s&q=info:%s' % (hsh, _urlib2.quote(url))
    headers={'User-Agent':user_agent,}
    request=_urlib1.Request(gurl,None,headers) #The assembled request
    u = _urlib1.urlopen(request)
    s = u.read().decode('utf-8')  # for py2, comment .decode() part
    #print(s)  # debug - response of server
    rank = s.strip()[9:]
    if rank == '':
        rank = 'None'
    if rank == 'None':
        rank = 'None'
    return rank


def  int_str(string, integer, factor):
    for i in range(len(string)) :
        integer *= factor
        integer &= 0xFFFFFFFF
        integer += ord(string[i])
    return integer


def hash_url(string):
    c1 = int_str(string, 0x1505, 0x21)
    c2 = int_str(string, 0, 0x1003F)

    c1 >>= 2
    c1 = ((c1 >> 4) & 0x3FFFFC0) | (c1 & 0x3F)
    c1 = ((c1 >> 4) & 0x3FFC00) | (c1 & 0x3FF)
    c1 = ((c1 >> 4) & 0x3C000) | (c1 & 0x3FFF)

    t1 = (c1 & 0x3C0) << 4
    t1 |= c1 & 0x3C
    t1 = (t1 << 2) | (c2 & 0xF0F)

    t2 = (c1 & 0xFFFFC000) << 4
    t2 |= c1 & 0x3C00
    t2 = (t2 << 0xA) | (c2 & 0xF0F0000)

    return (t1 | t2)


def check_hash(hash_int):
    hash_str = '%u' % (hash_int)
    flag = 0
    check_byte = 0

    i = len(hash_str) - 1
    while i >= 0:
        byte = int(hash_str[i])
        if 1 == (flag % 2):
            byte *= 2;
            byte = int(byte / 10) + byte % 10
        check_byte += byte
        flag += 1
        i -= 1

    check_byte %= 10
    if 0 != check_byte:
        check_byte = 10 - check_byte
        if 1 == flag % 2:
            if 1 == check_byte % 2:
                check_byte += 9
            check_byte >>= 1

    return '7' + str(check_byte) + hash_str

有人可以帮忙吗?

【问题讨论】:

  • 作为第一步,我会捕获异常以查看发生在哪个 URL 上
  • @Jasper 错误出现在所有网址上。谷歌似乎阻止了从某个 ip 进行一定数量的查询。你有任何 cmets 我该如何绕过它?
  • 由于阻止是在 Google 的控制之下,除了使用不同的 IP 之外,您无能为力。您是否违反了一些“合理使用”条件?
  • 是的,我也认为我必须找到一种机制来更改我的 ip @Jasper。你有什么建议吗?
  • 如果您使用家庭路由器(许多文件共享网站限制每个 IP 的下载),我认为您可以找到很多提示。

标签: python urllib


【解决方案1】:

问题不在于 IP 地址被阻止。我正在使用 Python3 并遇到同样的问题。我发现 Google 会阻止不会覆盖 User-Agent 和 Accept-Encoding 标头的 urllib。

它用于测试搜索的标题:

GET /search?q=f1+2015 HTTP/1.1
Accept-Encoding: identity
Connection: close
User-Agent: Python-urllib/3.4
Host: 127.0.0.1:8076

我将 'Accept-Encoding' 设置为 '' 并将 'User-Agent' 设置为 'testing' 并且 403 错误停止了。

【讨论】:

  • 您还可以在任何浏览器(例如 Firefox、Chrome 等)中使用 User-Agent 标头。
  • @ForceBru 嗯嗯!!说“你不能使用来自 urllib 的用户代理”更有用。
猜你喜欢
  • 2014-05-17
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多