Webscraping AttributeError:'NoneType'对象没有属性'text'

时间:2018-05-03 21:42:00

标签: python web-scraping

我正在从this page网上搜索足球赛程。

这是我的代码:

19:30 UNAM 1-4 America
21:30 Tijuana 1-1 Monterrey
19:30 Tigres --- Santos
21:30 Morelia --- Toluca
19:00 America --- UNAM
21:00 Monterrey --- Tijuana
12:00 Toluca --- Morelia
18:00 Santos --- Tigres
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    time = result.find('div', {'class':'hor'}).text
AttributeError: 'NoneType' object has no attribute 'text'

但是我收到了这个错误:

{{1}}

问题是,错误行似乎有效,因为它正在返回时间。

1 个答案:

答案 0 :(得分:0)

如果您只想解析时间表,则无法使用该选择器,因为排行榜还包含名为det的类的元素。

这是你可以做到的:

import requests
my_url = requests.get('http://www.vivoelfutbol.com.mx/futbolmexicano.php')

from bs4 import BeautifulSoup as soup
page_soup = soup(my_url.text, "html.parser")
containers = page_soup.find_all("div", {"class":"bordermitad"})[0].find_all("div", {"class":"det"})

for result in containers:
    time = result.find('div', {'class':'hor'}).text
    hometeam = result.find('a').text
    score = result.find('a', {'title':'Analisis y Antecedentes'}).text
    awayteam = result.find('div', {'class':'eqvc'}).text
    print(time + " " + hometeam + " " + score + " " + awayteam)

<强>输出

19:30 UNAM 1-4 America
21:30 Tijuana 1-1 Monterrey
19:30 Tigres --- Santos
21:30 Morelia --- Toluca
19:00 America --- UNAM
21:00 Monterrey --- Tijuana
12:00 Toluca --- Morelia
18:00 Santos --- Tigres