3 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
您找不到它的原因是您table_header
沒有找到任何東西,而您沒有從您那里得到任何東西的原因table_header
?是因?yàn)槟?code>404狀態(tài)代碼。您只需執(zhí)行 a.status_code
并打印即可檢查您的狀態(tài)代碼。
來(lái)源維基百科
HTTP 404、404 Not Found、404、404 Error、Page Not Found、File Not Found 或 Server Not Found 錯(cuò)誤消息是超文本傳輸協(xié)議 (HTTP) 標(biāo)準(zhǔn)響應(yīng)代碼,在計(jì)算機(jī)網(wǎng)絡(luò)通信中,表示瀏覽器已能夠與給定服務(wù)器通信,但該服務(wù)器找不到什么...
我對(duì)你的代碼做了一些修改并打印了它,status code
它說404
。
import requests
from bs4 import BeautifulSoup
link = "https://www.oddsportal.com/basketball/usa/nba/los-angeles-lakers-miami-heat-IqLamQfL/#over-under;1"
html_doc = requests.get(link)
print(html_doc.status_code)
odds_soup = BeautifulSoup(html_doc.content, 'html5lib')
table_header = odds_soup.find('div',{"id":"odds-data-table"})
'''
list = []
table_containers = []
for tag in table_header:
? ? table_containers += tag.find_all('div', {'class' : 'table-container'})
'''
輸出:
404
[Finished in 2.1s]

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
只需選擇它們即可:
soup.find_all("div", class_="table-header-light odd first")

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
為此需要使用 selenium 并傳遞到 BS4。然后 .append() 到您的列表或打印它。
driver = webdriver.Chrome()
driver.get('https://www.oddsportal.com/basketball/usa/nba/los-angeles-lakers-miami-heat-IqLamQfL/#over-under;1')
odds_soup = BeautifulSoup(driver.page_source , 'html.parser')
table_header = odds_soup.find_all("div", class_="table-header-light odd first")
for tag in table_header:
print(tag)
顯示器
<div class="table-header-light odd first"><strong><a href="" onclick="page.togleTableContent('P-201.50-0-0',this);return false;">Over/Under +201.5 </a></strong><span class="avg chunk-odd-payout"></span><span class="avg chunk-odd nowrp"></span><span class="avg chunk-odd nowrp"></span><span class="odds-cnt">(0)</span><span class="odds-co"><a class="more" href="" onclick="page.togleTableContent('P-201.50-0-0',this);return false;">Compare odds</a></span></div>
添加回答
舉報(bào)