我在使用 beautifulsoup 解析来自 html 的数据时遇到问题
I've got problem with parsing data from html using beautifulsoup
我正在尝试从 link 下载匹配列表:https://www.vitisport.cz/index.php?clanek=quicktips&sekce=fotbal&lang=pl。我一直收到错误消息:
Traceback (most recent call last):
File "C:/Users/jakub/PycharmProjects/proj2/main.py", line 9, in <module>
rows = table.find('td').find_all('tr')
AttributeError: 'NoneType' object has no attribute 'find'
我可以请教你吗?
这是我的代码:
main.py
import urllib.request
from bs4 import BeautifulSoup
from match import Match
matches= []
source = urllib.request.urlopen("https://www.vitisport.cz/index.php?clanek=quicktips&sekce=fotbal&lang=pl").read()
soup = BeautifulSoup(source, 'html.parser')
table = soup.find_all("table", {"class": "tabulkaquick"})[0].find('table')
rows = table.find('td').find_all('tr')
for row in rows:
cols = row.find_all("td")
matches.append(Match(cols[0].datetime,cols[1].text,cols[2].text,cols[3].int,cols[4].text,cols[5].text,cols[6].text))
with open('matches_list.txt','w') as f:
for match in matches:
print(match)
f.write(f"{match.date},{match.host},{match.guest},{match.goals1st},{match.goals2nd},{match.type},({match.index})\n")
和match.py:
from datetime import datetime
class Match:
def __init__(self, date, host, guest, goals1st, goals2nd, type, index):
self.date = datetime.strptime(date, '%d.%m.%Y')
self.host = host(str)
self.guest = guest(str)
self.goals1st = goals1st(int)
self.goals2nd = goals2nd(int)
self.type = type(str)
self.index = index(float)
def __repr__(self):
return self.date
return self.host
return self.guest
return self.goals1st
return self.goals2nd
return self.type
return self.index
你得到它的方式,table returns None
,所以你不能解析它,试试这个你会得到 table
soup = BeautifulSoup(source, 'html.parser')
table = soup.find("table", {"class": "tabulkaquick"})
rows = table.find_all('tr')
我正在尝试从 link 下载匹配列表:https://www.vitisport.cz/index.php?clanek=quicktips&sekce=fotbal&lang=pl。我一直收到错误消息:
Traceback (most recent call last):
File "C:/Users/jakub/PycharmProjects/proj2/main.py", line 9, in <module>
rows = table.find('td').find_all('tr')
AttributeError: 'NoneType' object has no attribute 'find'
我可以请教你吗?
这是我的代码: main.py
import urllib.request
from bs4 import BeautifulSoup
from match import Match
matches= []
source = urllib.request.urlopen("https://www.vitisport.cz/index.php?clanek=quicktips&sekce=fotbal&lang=pl").read()
soup = BeautifulSoup(source, 'html.parser')
table = soup.find_all("table", {"class": "tabulkaquick"})[0].find('table')
rows = table.find('td').find_all('tr')
for row in rows:
cols = row.find_all("td")
matches.append(Match(cols[0].datetime,cols[1].text,cols[2].text,cols[3].int,cols[4].text,cols[5].text,cols[6].text))
with open('matches_list.txt','w') as f:
for match in matches:
print(match)
f.write(f"{match.date},{match.host},{match.guest},{match.goals1st},{match.goals2nd},{match.type},({match.index})\n")
和match.py:
from datetime import datetime
class Match:
def __init__(self, date, host, guest, goals1st, goals2nd, type, index):
self.date = datetime.strptime(date, '%d.%m.%Y')
self.host = host(str)
self.guest = guest(str)
self.goals1st = goals1st(int)
self.goals2nd = goals2nd(int)
self.type = type(str)
self.index = index(float)
def __repr__(self):
return self.date
return self.host
return self.guest
return self.goals1st
return self.goals2nd
return self.type
return self.index
你得到它的方式,table returns None
,所以你不能解析它,试试这个你会得到 table
soup = BeautifulSoup(source, 'html.parser')
table = soup.find("table", {"class": "tabulkaquick"})
rows = table.find_all('tr')