Python的3​​ - 不能打印使用重新库

人气:802 发布:2022-09-16 标签: python-3.x python-requests beautifulsoup

问题描述

我有这样的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)

据打印出什么,当我听说它应打印< A HREF =/约/评论>意见< / A> A 我使用:结果要求:2.7.0结果beautifulsoup4:4.4.0结果的Python:3.4.3结果有关python空转:MacBook Pro的

It prints out nothing when I heard that it should print <a href="/about/comments">Comments</a> and a I am using: requests: 2.7.0 beautifulsoup4: 4.4.0 Python : 3.4.3 running on python Idle: Macbook Pro

推荐答案

re.compile()匹配大小写敏感默认。你必须设置标志 如re.I ,使其不区分大小写。请参见下面的演示例如:

re.compile() match case-sensitively by default. You got to set 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)

输出:

<a href="/about/comments">Comments</a>

770