如何使用python从嵌入式链接中提取链接



我有一个这样的字符串:

<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FDoctorTaniya%2Fposts%2F1906676949620646&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>

我想提取链接:

www.facebook.com/DoctorTaniya/posts/1906676949620646

如何编写一个python脚本来做到这一点?

我认为最好

用漂亮的汤代替。

要解析的文本是带有 srciframe 标记。您正在尝试在 src 属性中href=之后和&width之前检索 URL。

之后,您需要将 url 解码回文本。

首先,你把它扔进漂亮的汤里,并从中得到属性:

text = '<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FDoctorTaniya%2Fposts%2F1906676949620646&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>'
soup = BeautifulSoup(text)
src_attribute = soup.find("iframe")["src"]

然后你可以在这里使用正则表达式或使用.split()(相当hack(:

# Regex
link = re.search('.*?href=(.*)?&', src_attribute).group(1)
# .split()
link = src_attribute.split("href=")[1].split("&")[0]

最后,您需要使用 urllib2 解码 url

link = urllib2.unquote(link)

你完成了!

因此,生成的代码将是:

from bs4 import BeautifulSoup
import urllib2
import re
text = '<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FDoctorTaniya%2Fposts%2F1906676949620646&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>'
soup = BeautifulSoup(text)
src_attribute = soup.find("iframe")["src"]
# Regex
link = re.findall('.*?href=(.*)?&', src_attribute)[0]
# .split()
link = src_attribute.split("href=")[1].split("&")[0]
link = urllib2.unquote(link)

这里有一些关于正则表达式的有用信息,用于在 Python 中查找 url。

如果您编码的所有 url 都将在.php?href=后立即启动,则可以创建一个循环,该循环在找到?href=时停止并拆分字符串。

或者您可以使用$_GET[]并打印它,这是您可能想要阅读的其他帖子。

import re
string = '<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FDoctorTaniya%2Fposts%2F1906676949620646&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>'
m = re.search( r'href=https%3A%2F%2F(.*)&width', string)
str2 = m.group(1)
str2.replace('%2F', '/')

输出

>>> str2.replace('%2F', '/')
'www.facebook.com/DoctorTaniya/posts/1906676949620646'

使用BeautifulSoupreurllib的组合:

from bs4 import BeautifulSoup
import re, urllib
html = """
<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FDoctorTaniya%2Fposts%2F1906676949620646&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
<p>some other rubbish here</p>
"""
# da soup
soup = BeautifulSoup(html, 'html5lib')
# href, (anything not &) afterwards
rx = re.compile(r'href=([^&]+)')
for iframe in soup.findAll('iframe'):
    link = urllib.unquote(rx.search(iframe['src']).group(1))
    print(link)
    # https://www.facebook.com/DoctorTaniya/posts/1906676949620646

它解析DOM,查找 iframe,使用正则表达式分析它们并取消引号找到的 URL。因此,您不会直接对DOM进行操作。

最新更新