嗨,不是每次,但有时当我试图访问 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字典进行排序,我希望它按该列表的第一个值排序.例如:
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)
非常感谢.