我正试图从某个网站上刮掉下注赔率。目前,我的代码可以打印出游戏的最后几率,但不能全部打印出来。关于我在做什么错的任何想法吗?
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
my_url = 'https://www.sportsbet.com.au/betting/australian-rules'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
soup = BeautifulSoup(page_html, "html.parser")
price_texts = soup.findAll("div",{"class":"priceText_f71sibe"})
filename = "odds.csv"
f = open(filename,"w")
headers = "odds team 1\n"
f.write(headers)
for price_text in price_texts:
odds = price_text.span.text
print("odds are: "+odds)
f.write(odds)
f.close()
答案 0 :(得分:0)
当然,它仅显示最后几率,因为您在print
循环之外编写了f.write()
和for
部分。 odds
的值每次都会改变,直到循环停止为止。您应该通过在print
和f.write(odds)
上添加缩进来使它们进入循环:
for price_text in price_texts:
odds = price_text.span.text
print("odds are: "+ odds)
f.write(odds)