1 回答

TA貢獻1816條經驗 獲得超4個贊
問題在于url您提供的。你錯過了一個=之后code。
更改naver_index = 'http://finance.naver.com/sise/sise_index_day.nhn?code' + index_cd + '&page=' + str(page_n)為naver_index = 'http://finance.naver.com/sise/sise_index_day.nhn?code=' + index_cd + '&page=' + str(page_n)
這是工作代碼:
index_cd = 'KPI200'
page_n = 1
naver_index = 'http://finance.naver.com/sise/sise_index_day.nhn?code=' + index_cd + '&page=' + str(page_n)
from urllib.request import urlopen
source = urlopen(naver_index).read()
import bs4
source = bs4.BeautifulSoup(source, 'lxml')
td = source.find_all('td')
len(td)
# /html/body/div/table[1]/tbody/tr[3]/td[1] # this is XPath
print(source.find_all('table')[0].find_all('tr')[2].find_all('td')[0])
輸出:
<td class="date">2020.09.29</td>
如果您只想顯示日期,請將最后一行更改為:
print(source.find_all('table')[0].find_all('tr')[2].find_all('td')[0].text)
輸出:
2020.09.29
希望這對你有幫助!
添加回答
舉報