我嘗試使用 Python 網路抓取從網站
謝謝你的幫助!!
uj5u.com熱心網友回復:
您可以下載 CSV 格式的資料,例如使用pandas. 只需修改網址:
import pandas as pd
url = "https://stooq.com/q/d/l/?s=4604.n&d1=20200101&d2=20220507&i=d"
df = pd.read_csv(url)
print(df.head().to_markdown())
列印前 5 行:
| 日期 | 打開 | 高的 | 低的 | 關閉 | |
|---|---|---|---|---|---|
| 0 | 2020-01-02 | 115.47 | 115.47 | 115.47 | 115.47 |
| 1 | 2020-01-03 | 115.4 | 115.4 | 115.4 | 115.4 |
| 2 | 2020-01-07 | 115.46 | 115.46 | 115.46 | 115.46 |
| 3 | 2020-01-08 | 115.61 | 115.61 | 115.61 | 115.61 |
| 4 | 2020-01-10 | 116.91 | 116.91 | 116.91 | 116.91 |
uj5u.com熱心網友回復:
現在我的完整代碼是:
from bs4 import BeautifulSoup
import requests
import datetime as dt
# the investment funds list
number_to_symbol = {
'1': '2711.n',
'2': '3187.n',
'3': '1472.n',
'4': '4604.n',
'5': '2735.n'
}
def url_get(num):
## Make URL address with the investment fund symbol and dates range (01.01.2020 - today)
symbol = number_to_symbol[str(num)]
end_time = f"20{dt.date.today().strftime('%y%m%d')}"
return f'https://stooq.com/q/d/l/?s={symbol}&d1=20200101&d2={end_time}&i=d'
url_name = url_get(4)
print(f'URL to web-scraping: {url_name}')
df = pd.read_csv(url_name)
print(df.head().to_markdown())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471077.html
上一篇:使用scrapy抓取JSON時出現此錯誤:Spidermustreturnrequest,item,orNone,got'str'
