第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用美湯進(jìn)行網(wǎng)頁抓?。w育數(shù)據(jù))

使用美湯進(jìn)行網(wǎng)頁抓取(體育數(shù)據(jù))

楊__羊羊 2022-01-11 17:18:42
當(dāng)我嘗試加載此代碼時(shí),出現(xiàn)兩個(gè)錯(cuò)誤。1:第一個(gè)是我無法正確抓取name_text的數(shù)據(jù)。2:我收到 team = name_text.div.text 的縮進(jìn)錯(cuò)誤。我知道這可能很容易解決,但我嘗試了不同的縮進(jìn),但似乎沒有任何效果。在網(wǎng)站上,我想抓取球隊(duì)名稱和賠率。<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是從網(wǎng)站上復(fù)制的。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()任何幫助都會(huì)很棒。干杯。
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

您的for loop縮進(jìn)不正確。正確的縮進(jìn)是:


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()

隊(duì)前有 4 個(gè)空格和賠率。請閱讀Python ForLoop 文檔。


此外,沒有price_texts變量。當(dāng)你做findAll時(shí)你需要分配它,你忘記了一個(gè)'S':


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

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


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

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

你能試試這個(gè)嗎?


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()


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

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

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


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()


查看完整回答
反對 回復(fù) 2022-01-11
  • 3 回答
  • 0 關(guān)注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號