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

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

無法在頁面上找到 div 類

無法在頁面上找到 div 類

眼眸繁星 2023-10-26 16:44:06
我無法在以下代碼中找到 div 類。網址如下:https://www.oddsportal.com/basketball/usa/nba/los-angeles-lakers-miami-heat-IqLamQfL/#over-under;1我的代碼如下:html_doc = urllib.request.urlopen(new_url).read()odds_soup = BeautifulSoup(html_doc, 'html.parser')table_header = odds_soup.find_all('div', {'id' : "odds-data-table", 'class' : 'bt-2'})list = []table_containers = []for tag in table_header:    table_containers += tag.find_all('div', {'class' : 'table-container'})但代碼只返回 table_containers 的空列表。我不知道為什么,非常感謝一些幫助。經查看該網站,它看起來像這樣:
查看完整描述

3 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

您找不到它的原因是您table_header沒有找到任何東西,而您沒有從您那里得到任何東西的原因table_header?是因為您有404狀態代碼。您只需執行 a.status_code并打印即可檢查您的狀態代碼。

來源維基百科

HTTP 404、404 Not Found、404、404 Error、Page Not Found、File Not Found 或 Server Not Found 錯誤消息是超文本傳輸協議 (HTTP) 標準響應代碼,在計算機網絡通信中,表示瀏覽器已能夠與給定服務器通信,但該服務器找不到什么...

我對你的代碼做了一些修改并打印了它,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]


查看完整回答
反對 回復 2023-10-26
?
浮云間

TA貢獻1829條經驗 獲得超4個贊

只需選擇它們即可:

soup.find_all("div", class_="table-header-light odd first")


查看完整回答
反對 回復 2023-10-26
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

為此需要使用 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>



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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