My code is below which extract all the elements
for link in soup.find_all('a', href=True):
print(link['href'])
Output
https://www.example.com/author/1/
https://www.example.com/about/2/
https://www.example.com/author/3/
type of (link['href'])
<cls str>
<cls str>
<cls str>
I need to extract the url which contains 'about'
I tried with print(link['href'] if 'about' in link)
which thrown eror
My expected out
https://www.example.com/about/2/
A conditional expression requires an else
clause to specify what the expression should return when the condition is false.
But you don't want to print anything in that case. So use an if
statement around the print()
call.
for link in soup.find_all('a', href=True):
if 'about' in link['href']:
print(link['href'])
You could also do the matching in the soup.find_all()
call.
for link in soup.find_all('a', href=re.compile(r'about')):
print(link['href'])
You are searching for 'about' in link whereas it seems the word about is found in link['href']. So try to update the if condition as below.
print(link['href'] if 'about' in link['href'] else '')
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.