我有这个代码:
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)
当我听说它应该打印时,它什么也没打印出来<a href="/about/comments">Comments</a>
and a
我在用:
请求:2.7.0
美丽汤4:4.4.0
蟒蛇:3.4.3
在 python Idle 上运行:Macbook Pro
re.compile()
默认情况下区分大小写。你必须设置标志re.I https://docs.python.org/2/library/re.html#re.I使其不区分大小写。请参阅以下演示示例:
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)