亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

試圖抓取頁面,但缺少一個

試圖抓取頁面,但缺少一個

慕雪6442864 2023-09-26 14:31:21
index_cd = 'KPI200'page_n = 1naver_index = 'http://finance.naver.com/sise/sise_index_day.nhn?code' + index_cd + '&page=' + str(page_n)from urllib.request import urlopensource = urlopen(naver_index).read()import bs4source = bs4.BeautifulSoup(source, 'lxml')td = source.find_all('td')len(td)# /html/body/div/table[1]/tbody/tr[3]/td[1]  # this is XPathsource.find_all('table')[0].find_all('tr')[2].find_all('td')[0]我以為輸出會是這樣的:<td class="date">2020.09.29</td>但結果是這樣的:<td class="date"> </td>和'\xa0'之間有一個。<td class="date"</td>我需要提取該日期。這種情況該如何解決呢?
查看完整描述

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

希望這對你有幫助!


查看完整回答
反對 回復 2023-09-26
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號