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

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

使用美湯進行網頁抓?。w育數據)

使用美湯進行網頁抓?。w育數據)

楊__羊羊 2022-01-11 17:18:42
當我嘗試加載此代碼時,出現兩個錯誤。1:第一個是我無法正確抓取name_text的數據。2:我收到 team = name_text.div.text 的縮進錯誤。我知道這可能很容易解決,但我嘗試了不同的縮進,但似乎沒有任何效果。在網站上,我想抓取球隊名稱和賠率。<div class="size14_f7opyze Endeavour_fhudrb0 medium_f1wf24vo participantText_fivg86r" data-automation-id="participant-one">Orlando Magic</div><div class="priceText_f71sibe"><span class="size14_f7opyze medium_f1wf24vo priceTextSize_frw9zm9" data-automation-id="price-text">5.85</span></div>上面的html是從網站上復制的。from bs4 import BeautifulSoupfrom urllib.request import urlopen as uReqmy_url = 'https://www.sportsbet.com.au/betting/basketball-us'uClient = uReq(my_url)page_html = uClient.read()uClient.close()soup = BeautifulSoup(page_html, "html.parser")price_text = soup.findAll("div",{"class":"priceText_f71sibe"})name_text = soup.findAll("div",{"class":"size14_f7opyze Endeavour_fhudrb0 medium_f1wf24vo participantText_fivg86r"})filename = "odds.csv"f = open(filename,"w")headers = "Team, odds_team\n"print(name_text)f.write(headers)for price_text in price_texts:team = name_text.div.textodds = price_text.span.textprint(odds)print(team + odds)f.write(team + "," + odds + "\n")f.close()任何幫助都會很棒。干杯。
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

您的for loop縮進不正確。正確的縮進是:


for price_text in price_texts:

    team = name_text.div.text

    odds = price_text.span.text

    team = name_text.div.text

    odds = price_text.span.text


    print(odds)

    print(team + odds)

    f.write(team + "," + odds + "\n")

f.close()

隊前有 4 個空格和賠率。請閱讀Python ForLoop 文檔。


此外,沒有price_texts變量。當你做findAll時你需要分配它,你忘記了一個'S':


price_texts = soup.findAll("div",{"class":"priceText_f71sibe"})

最后一件事,考慮使用with而不是open()和.close()寫入您的文件。


查看完整回答
反對 回復 2022-01-11
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

你能試試這個嗎?


from bs4 import BeautifulSoup

from urllib.request import urlopen as uReq

my_url = 'https://www.sportsbet.com.au/betting/basketball-us'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()


soup = BeautifulSoup(page_html, "html.parser")


price_texts = soup.findAll("div",{"class":"priceText_f71sibe"})

name_texts = soup.findAll("div",{"class":"size14_f7opyze Endeavour_fhudrb0 medium_f1wf24voparticipantText_fivg86r"})

filename = "odds.csv"

f = open(filename,"w")

headers = "Team, odds_team\n"

print(name_text)

f.write(headers)


odds =''

team=''

for price_text in price_texts:

    odds = price_text.text

for name_text in name_texts:

    team = name_text.text

print(odds)

print(team + odds)

f.write(team + "," + odds + "\n")

f.close()


查看完整回答
反對 回復 2022-01-11
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

我在想你可以做的只是迭代并將它們存儲到列表中,然后寫入文件。不幸的是,我無法在工作中訪問該站點,因此無法測試代碼,但我相信這應該會提供您正在尋找的輸出:


from bs4 import BeautifulSoup

from urllib.request import urlopen as uReq

import csv

from itertools import zip_longest


my_url = 'https://www.sportsbet.com.au/betting/basketball-us'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()


soup = BeautifulSoup(page_html, "html.parser")


price_text = soup.findAll("span",{"data-automation-id":"price-text"})

name_text = soup.findAll("div",{"data-automation-id":"participant-one"})


team_list = [ name.text.strip() for name in name_text ]

odds_list = [ price.text.strip() for price in price_text ]


d = [team_list, odds_list]

export_data = zip_longest(*d, fillvalue = '')

with open('odds.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:

      wr = csv.writer(myfile)

      wr.writerow(("Team", "odds_team"))

      wr.writerows(export_data)

myfile.close()


查看完整回答
反對 回復 2022-01-11
  • 3 回答
  • 0 關注
  • 247 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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