Python BeautifulSoup AttributeError:&x27;NavigableString';对象没有属性';查找所有';
我正试图从这个URL中刮取数据,但刮取器的这一部分出现了错误,下面是完整的代码块Python BeautifulSoup AttributeError:&x27;NavigableString';对象没有属性';查找所有';,python,beautifulsoup,Python,Beautifulsoup,我正试图从这个URL中刮取数据,但刮取器的这一部分出现了错误,下面是完整的代码块 if table.find_all('tr'): 注意:我以前构建它时没有使用if/elif/else逻辑,只是find_all('tr'),但它会产生相同的错误 Traceback (most recent call last): File "statbunker.py", line 217, in <module> if table.find_all('tr'):
if table.find_all('tr'):
注意:我以前构建它时没有使用if/elif/else
逻辑,只是find_all('tr')
,但它会产生相同的错误
Traceback (most recent call last):
File "statbunker.py", line 217, in <module>
if table.find_all('tr'):
File "/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/bs4/element.py", line 921, in __getattr__
raise AttributeError(
AttributeError: 'NavigableString' object has no attribute 'find_all'
在这里,使用.find()
将只返回标记/navigablestring组合,您必须使用.find\u all()
进行迭代:
home_substititions = soup_loop.find_all('table', {'id': 'homeSubs'})
for table in home_substititions:
# ....
做了一些小的改变。问题是您在stuff.find('sth')中为表添加了
,但您只找到了一个元素,因此不需要循环
import requests
from bs4 import BeautifulSoup
link = 'https://rugby.statbunker.com/competitions/MatchDetails/World-Cup-2019/Japan-VS-Russia?comp_id=606&match_id=39737&date=20-Sep-2019'
response = requests.get(link)
html_loop = response.content
soup_loop = BeautifulSoup(html_loop, 'html.parser')
home_substititions = soup_loop.find('table', {'id': 'homeSubs'})
table = home_substititions.find('tbody')
print(table)
if table.find_all('tr'):
for row in table.find_all('tr'):
substitutionEvent = {}
substitutionEvent['uuid'] = uuid.uuid1()
substitutionEvent['playerIn'] = row.find_all('td')[2].text
substitutionEvent['playerOut'] = row.find_all('td')[4].text
if int(row.find_all('td')[0].text.split('`')[0]):
substitutionEvent['subTime'] = game['gameTime'] + timedelta.Timedelta(minutes=int(row.find_all('td')[0].text.split('`')[0]))
else:
substitutionEvent['subTime'] = ''
homeSubstitutionEvents.append(substitutionEvent)
elif table.find('tr'):
for row in table.find('tr'):
substitutionEvent = {}
substitutionEvent['uuid'] = uuid.uuid1()
substitutionEvent['playerIn'] = row.find_all('td')[2].text
substitutionEvent['playerOut'] = row.find_all('td')[4].text
if int(row.find_all('td')[0].text.split('`')[0]):
substitutionEvent['subTime'] = game['gameTime'] + timedelta.Timedelta(minutes=int(row.find_all('td')[0].text.split('`')[0]))
else:
substitutionEvent['subTime'] = ''
homeSubstitutionEvents.append(substitutionEvent)
else:
pass
问题是home\u子项不是一个漂亮的soup类,也不是soup\u循环
type (home_substititions)
type (soup_loop)
输出将是
<class 'bs4.element.Tag'>
<class 'bs4.BeautifulSoup'>
要使代码正常工作,您需要将“查找”和“全部查找”应用于原始汤这对您有帮助吗?@Simplecode不完全是。。。我能够找到表,但是我在表内部的标记上得到了一个错误,并且标记上没有可用于进一步选择的id
或其他属性
<class 'bs4.element.Tag'>
<class 'bs4.BeautifulSoup'>