循环和网页抓取。如何抓取多个元素

时间:2019-04-11 01:45:21

标签: python html web-scraping beautifulsoup

我正试图从某个网站上刮掉下注赔率。目前,我的代码可以打印出游戏的最后几率,但不能全部打印出来。关于我在做什么错的任何想法吗?

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

1 个答案:

答案 0 :(得分:0)

当然,它仅显示最后几率,因为您在print循环之外编写了f.write()for部分。 odds的值每次都会改变,直到循环停止为止。您应该通过在printf.write(odds)上添加缩进来使它们进入循环:

for price_text in price_texts:
    odds = price_text.span.text
    print("odds are: "+ odds)
    f.write(odds)