靓汤变跨度类

2024-04-19 23:07:53 发布

您现在位置:Python中文网/ 问答频道 /正文

不知道你能不能帮我清理一下网页。在

下面是我要从中获取数据的span类。问题是在span类中存在一个随机数,用于不同的数据点。在

我知道“price val”部分对于所有迭代都是相同的,但是我无法计算出在获取数据时如何搜索这个值。在

   <span class="price-val_196775436 odd-val ib right">
    2.47
    </span>

到目前为止我的代码

^{pr2}$

我得到的错误

Traceback (most recent call last):
  File "C:\Users\James\Desktop\NFLsportsbet.py", line 23, in <module>
    B = item.find('span', {'class': 'price-val'}).text
AttributeError: 'NoneType' object has no attribute 'text'

Tags: 数据代码textright网页most错误val
1条回答
网友
1楼 · 发布于 2024-04-19 23:07:53

使用解析器库,例如lxml,可能需要使用regex或lambda-

import requests,re
from  bs4  import  BeautifulSoup

url ="http://www.sportsbet.com.au/betting/american-football"
r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
g_data = soup.find_all("div", {"class": "accordion-body"})



for items in g_data:
    print items.find('span', {'class': 'team-name ib'}).text
    print items.find('span', {'class': lambda L: L and L.startswith('price-val_')}).text
    #print items.find('span', {'class': re.compile('price-val_*')}).text  #or regex like this

它印出来了

^{pr2}$

相关问题 更多 >