【问题标题】:Use Regex with Python to get an specifc part of the iframe src使用 Regex 和 Python 获取 iframe src 的特定部分
【发布时间】:2019-03-26 18:45:51
【问题描述】:

我尝试捕获我想要更改的 iframe src 内容。我无法直接访问 HTML,我从 API 获取 HTML。

您可以在下面看到一些 iframe 示例:

<iframe src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490">
<iframe allowfullscreen="" frameborder="0" height="276" mozallowfullscreen="" scrolling="no" src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/%20f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490"></iframe>

我还有很多其他类型的 iframe 示例,它们唯一的共同点就是这部分 src 内容 https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302

我创建以下代码来查找元素:

// some code
regex_page_embed = r"http.?://fast\.player\.liquidplatform\.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/*"
soup = BeautifulSoup(page_html, 'html.parser')
page_elements = list(soup.children)
for element in page_elements:
    try:
        s1 = re.search(regex_page_embed, str(element))
        if s1:
            print(s1)
            print(s1.group())

之后,我创建了更多可以使用的代码,并使用 API 有效地更改了 HTML,我认为没有必要把它放在这里。 但是当我使用时:

print(s1)
print(s1.group())

我得到了以下结果:

<_sre.SRE_Match object; span=(686, 771), match='https://fast.player.liquidplatform.com/pApiv2/emb>
https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/
<_sre.SRE_Match object; span=(126, 211), match='https://fast.player.liquidplatform.com/pApiv2/emb>
https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/
<_sre.SRE_Match object; span=(686, 771), match='https://fast.player.liquidplatform.com/pApiv2/emb>
https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/
<_sre.SRE_Match object; span=(227, 312), match='https://fast.player.liquidplatform.com/pApiv2/emb>
https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/

我想获取 iframe src 内容的最后一部分。在下面的例子中

<iframe src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490">

f2c5f6ca3a4610c55d70cb211ef9d977是我想要的部分。

print(s1)print(s1.group()) 不显示 src 内容的最后一部分,如何获取最后一部分iframe src 内容?

【问题讨论】:

  • 在正则表达式中,将末尾的星号更改为(.*?)(?=\")
  • 使用正则表达式解析 html 内容的相关阅读:stackoverflow.com/a/1732454/9183344
  • 我只是使用 bs4 解析 iframe,然后提取 src 文本内容并从那里开始......
  • 我尝试先使用 bs4 来获取内容,但我发现使用 regex 获得的结果比 bs4 多。我调查了为什么会发生这种情况,我发现使用 javascript document.write 在页面中插入了一些 iframe。这样只有 regex 能找到它,bs4 也找不到。
  • 对,因为它是动态内容,您应该使用不同的模块,例如 seleniumrequests-html。我真的很惊讶你能够在bs4 提取的内容中获得 iframe。

标签: python regex iframe


【解决方案1】:

&lt;iframe 标签和src 标签之间有任何可选内容的同时捕获整个网址的更好的正则表达式是这样的,

<iframe .*?\bsrc="(https?://fast\.player\.liquidplatform\.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/[^"]+)

使用此正则表达式匹配并从 group1 中捕获您的 url。

Online Demo

这是您更新后的 Python 代码,

regex_page_embed = r'<iframe .*?\bsrc="(https?://fast\.player\.liquidplatform\.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/[^"]+)'
soup = BeautifulSoup(page_html, 'html.parser')
page_elements = list(soup.children)
for element in page_elements:
    try:
        s1 = re.search(regex_page_embed, str(element))
        if s1:
            print(s1.group(1)) # extract url using first group

【讨论】:

    【解决方案2】:

    使用r'&lt;iframe src="[^"]*/([^"]+)"' 作为搜索模式。

    示例:

    >>> text = """<iframe src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490">"""
    >>> pat = r'<iframe src="[^"]*/([^"]+)"'
    >>> search = re.search(pat, text)
    >>> search[1]
    'f2c5f6ca3a4610c55d70cb211ef9d977'
    >>> 
    

    【讨论】:

    • 我现在编辑我的问题,我包含第二个 iframe 示例。我忘了提到我在 HTML 中包含了另一种类型的 iframe。如果所有 iframe 仅基于第一个 iframe 示例,您的答案将是正确的。我的页面中有另一个 iframe 示例与我提供的 2 个示例完全不同,唯一的共同部分是 iframe src 内容。
    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多