Is there any way to just get the first result back from for i soup.select(table)
? I just want the first table, every table after it should be ignored. The code is followed by an if statement: if i.find('th', text = 'Foo'):
TLDR;
Looking for something like this: if i[0].find('th', text = 'Foo'):
One way is to break
right after the first iteration:
for i in soup.select('table'):
if i.find('th', text = 'Foo'):
...
break
Another one is to chain methods, and catch exception if element not found:
try:
el = soup.select('table')[0].find('th', text='Foo')
except AttributeError, TypeError:
print('element not found')
Note: soup.select('table')[0]
and soup.find('table')
give the same result
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.