Python 3 - Unable to print using re library
I have this code:
import requests
from bs4 import BeautifulSoup
import re
url = "http://www.rockefeller.edu/research/areas/summary.php?id=1"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
for x in (soup.find_all(string=re.compile('comment'))):
print(x.parent)
print(x.parent.name)
It doesn't print anything when I heard it should print <a href="/about/comments">Comments</a>
and a
I use:
requests: 2.7.0
beautifulsoup4: 4.4.0
Python: 3.4.3
works on python Idle: Macbook Pro
+3
source to share
1 answer
re.compile()
The default is random. You must set a flag re.I
to make it case insensitive. See the following demo example:
import requests
from bs4 import BeautifulSoup
import re
url = "http://www.rockefeller.edu/research/areas/summary.php?id=1"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
for x in (soup.find_all(True,text=re.compile(r'comment', re.I))):
print(x)
output:
<a href="/about/comments">Comments</a>
+1
source to share