Python的3​​ - 不能打印使用重新库 [英] Python 3 - Can't print using the re library

查看:124
本文介绍了Python的3​​ - 不能打印使用重新库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于Python的3​​ - 不能打印使用重新库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆