简体   繁体   English

Python 3 - 无法使用re库进行打印

[英]Python 3 - Can't print using the 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 prints out nothing when I heard that it should print <a href="/about/comments">Comments</a> and a I am using: 它打印出没事的时候我听见他要打印<a href="/about/comments">Comments</a>a我使用:
requests: 2.7.0 要求:2.7.0
beautifulsoup4: 4.4.0 beautifulsoup4:4.4.0
Python : 3.4.3 Python:3.4.3
running on python Idle: Macbook Pro 在python上运行Idle:Macbook Pro

re.compile() match case-sensitively by default. re.compile()默认情况下匹配大小写。 You got to set flag re.I to make it case-insensitive. 你必须设置标志re.I以使其不区分大小写。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM