关于“在一个芬德尔州”

2024-04-26 11:08:04 发布

您现在位置:Python中文网/ 问答频道 /正文

在python搜索html源代码的regex findall语句中使用“in”时遇到了一个问题。你知道吗

我正在搜索一些html源代码,似乎无法在findall语句中使用引号(“)。由于某些无法更改的要求,我无法使用像beautifulsoup这样的外部库来帮助搜索。我已将变量名更改为搜索。你知道吗

from re import *

def suncorp_find():

    # Setup to find information
    suncorp_file = open('suncorp.html')
    contents_suncorp = suncorp_file.read()

    # Search the HTMl files to find the data
    suncorp_titles = findall(r"\"event-title\">(\w )+", contents_suncorp)

    print(suncorp_titles)

suncorp_find()

我希望得到一个列表中的项目,但我只是得到一个空列表。当只搜索事件标题时,我得到多个带有搜索标题列表的项目。你知道吗

提前谢谢你的帮助

<h6 class="event-title">Queensland Reds v Jaguares</h6>

Tags: theto项目event列表源代码titlehtml
2条回答

你应该引用"符号。你知道吗

from re import findall

tmp = """<some_tag name="event-title">Some text 1</some-tag>
<some_tag name="event-title">Some text 2</some-tag>
<some_tag name="event-title">Some text 3</some-tag>"""

result = findall("\"event-title\">([\w ]+)", tmp)

输出:

['Some text 1', 'Some text 2', 'Some text 3']

另外,我建议您使用regex test website来验证您的表达式。你知道吗

使用此正则表达式:

suncorp_titles = findall(r"\"event-title\">(\w.*?)<", contents_suncorp)

或者为什么不低于??我已删除\w检查。我不知道你是否真的需要它。你知道吗

suncorp_titles = findall(r"\"event-title\">(.*?)<", contents_suncorp)

我接受了意见:

<h6 class="event-title">Queensland Reds v Jaguares</h6>
<h6 class="event-title">testing line two</h6>

输出:

['Queensland Reds v Jaguares', 'testing line two']

相关问题 更多 >