我一直能夠毫無問題地運行這行代碼,將
但是,它現在回傳錯誤:
HTTPError: HTTP Error 500: Internal Server Error
我很確定這是因為網站上新實施的 cookie 要求(見圖)。還有沒有辦法用read_html(). 我找不到read_html()與 cookie相關的任何問題,但此執行緒可能有助于解決(無法捕獲 HTTP 錯誤 500:內部服務器錯誤)。我希望read_html()庫有一個簡單的解決方案,但我可能不得不使用requests/ beautifulsoup。
任何人都可以幫助我從該鏈接回傳表格作為輸出嗎?現在可能無法使用以下一行代碼read_html():
uj5u.com熱心網友回復:
您需要注入 User-Agent
import requests
import pandas as pd
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
url = "https://www.bankofengland.co.uk/boeapps/database/Rates.asp?Travel=NIxIRx&into=GBP"
req = requests.get(url,headers=headers)
table = pd.read_html(req.text, attrs = {"class":"table"} )
df = table[0]#.to_csv('score.csv',index = False)
print(df)
輸出:
Currency £1 13 Oct 2021 £1 52 wk-high £1 52 wk-low
0 Australian Dollar 1.8493 1.9103 1.7434
1 Canadian Dollar 1.6970 1.7769 1.6880
2 Chinese Yuan 8.7618 9.1286 8.5980
3 Czech Koruna 29.8762 30.7526 28.6068
4 Danish Krone 8.7644 8.7869 8.1111
5 Euro 1.1779 1.1815 1.0902
6 Hong Kong Dollar 10.6069 11.0357 10.0020
7 Hungarian Forint 424.4322 430.6888 386.7975
8 Indian Rupee 102.7158 105.1990 94.8636
9 Israeli Shekel 4.4054 4.6524 4.2677
10 Japanese Yen 154.5301 156.2211 134.9730
11 Malaysian ringgit 5.6713 5.9233 5.3545
12 New Zealand Dollar 1.9596 2.0030 1.8646
13 Norwegian Krone 11.6286 12.3972 11.4222
14 Polish Zloty 5.3925 5.4644 4.8446
15 Russian Ruble 98.2299 107.0946 96.5495
16 Saudi Riyal 5.1133 5.3296 4.8394
17 Singapore Dollar 1.8435 1.8907 1.7564
18 South African Rand 20.2195 21.5353 19.1216
19 South Korean Won 1621.0728 1630.4056 1442.6832
20 Swedish Krona 11.8859 12.0659 11.0226
21 Swiss Franc 1.2627 1.3035 1.1753
22 Taiwan Dollar 38.2719 39.7162 36.8693
23 Thai Baht 45.2616 46.3459 39.6758
24 Turkish Lira 12.3676 12.3676 9.6744
25 US Dollar 1.3633 1.4211 1.2904
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317029.html
上一篇:抓取跨度標題
