小编Joe*_*sed的帖子

如何绕过 HTTP 错误 403: Forbidden with urllib.request using Python 3

嗨,不是每次,但有时当我试图访问 LSE 代码时,我会被抛出每个烦人的 HTTP 错误 403:禁止消息。

任何人都知道我如何仅使用标准 python 模块来解决这个问题(遗憾的是没有漂亮的汤)。

import urllib.request

url = "http://www.londonstockexchange.com/exchange/prices-and-markets/stocks/indices/ftse-indices.html"
infile = urllib.request.urlopen(url) # Open the URL
data = infile.read().decode('ISO-8859-1') # Read the content as string decoded with ISO-8859-1

print(data) # Print the data to the screen
Run Code Online (Sandbox Code Playgroud)

但是,时不时地这是我显示的错误:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/programming_practice/Assessment/Summative/removingThe403Error.py", line 5, in <module>
    webpage = urlopen(req).read().decode('ISO-8859-1')
  File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 469, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 579, …
Run Code Online (Sandbox Code Playgroud)

python urllib urllib3 http-status-code-403 python-3.x

5
推荐指数
2
解决办法
1万
查看次数

当值是一个列表时,如何按值对python中的字典进行排序,我想按该列表的第一个索引对其进行排序

如果值是一个列表,是否可以按值对python字典进行排序,我希望它按该列表的第一个值排序.例如:

data = {
"Joe" : [1, "Joe", "password", "Joe@Email.com"], 
"Toby" : [2, "Toby", "password", "Toby@Email.com"], 
"John" : [4, "John", "password", "John@Email.com"], 
"Julie" : [3, "Julie", "password", "Julie@Email.com"]
}
Run Code Online (Sandbox Code Playgroud)

我希望它是这样的:哪里'我'是关键

for i in range(len(data)):
    print("UserID: ", str(data[i][0]), ". Username: ", data[i][1])

>> UserID: 1. Username: Joe
>> UserID: 2. Username: Toby
>> UserID: 3. Username: Julie
>> UserID: 4. Username: John
Run Code Online (Sandbox Code Playgroud)

非常感谢.

python python-3.x

4
推荐指数
1
解决办法
105
查看次数