HTTPError:导入urllib.request时禁止

时间:2018-10-10 16:22:57

标签: python

我去年为一个Python项目编写了此货币转换代码。我再次运行它并确保它仍然可以工作,因为我想将其作为Github上的项目列出,但是我收到了HTTPError:Forbidden。我已经搜索并找到了其他与此相关的帖子,但是这些帖子都没有为我工作。有什么方法可以使用我拥有的内容从相同的URL进行抓取吗?还是我需要在没有urllib的情况下重新编写它,因为我知道它可以防止由于漫游器的影响(从我读过的与此主题相关的其他一篇文章中了解到)。

代码:

    ognumber = str
    import urllib.request

    while True:
        ogcurrency = str((input("What is the original currency? "))).upper()
        newcurrency = str((input("What currency do you want to convert to? ")))\
        .upper()
        # Asks the first two questions

            while True:
                ognumber_str = input("How much do you want to convert (int)? ")
                    # Asks how much needs to be converted
                if(ognumber_str.isdigit()):
                    ognumber_int = int(ognumber_str)
                    break
                    # if the is str is an integer, continue, if not, ask to try again
                else:
                    print("The value you input must be an integer. Please try again.")


            if ognumber_str != str:

                url = \
                "https://finance.google.com/finance/converter?a={:d}&from={:s}&to={:s}\
                ".format(ognumber_int, ogcurrency, newcurrency)
                    # calls to conversion api
                response = urllib.request.urlopen(url)
                result = str(response.read())

                index_back = result.find("</span") #end of api we want
                index = result.find("span") #beginning of api section we want
                index_s = result[index+15:index_back] #spliced more
                index_flt = float(index_s[:-4]) #the floating integer values of the str

                index_currency = index_s[-3:] # string of the currency
                print("{} {} is {:.2f} {}".format(ognumber_int, ogcurrency, index_flt,\
          index_currency)) #formatting for expected final result


                leave = input("Do you want to convert another currency? ").upper()
                #input no in any format to leave the program

                if leave == "NO":
                    break

                else:
                    continue

错误:

Traceback (most recent call last):

File "<ipython-input-19-1cc53791cbcf>", line 1, in <module>
runfile('C:/Users/laddand1/TEST.py', wdir='C:/Users/laddand1')

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/laddand1/TEST.py", line 37, in <module>
  response = urllib.request.urlopen(url)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 223, in urlopen
  return opener.open(url, data, timeout)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 532, in open
  response = meth(req, response)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 642, in http_response
  'http', request, response, code, msg, hdrs)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 570, in error
  return self._call_chain(*args)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 504, in _call_chain
  result = func(*args)

File "C:\Users\laddand1\AppData\Local\Continuum\anaconda3\lib\urllib\request.py", line 650, in http_error_default
  raise HTTPError(req.full_url, code, msg, hdrs, fp)

HTTPError: Forbidden

0 个答案:

没有答案