如果您只想将四个放在一起,您可以使用 BeautifulSoup css-selectors 从包含四个 iframe 的第二个表中获取数据,特别是 nth-of-type(2) 拉第二个表:
from bs4 import BeautifulSoup
import requests
html = requests.get("http://kathaltamil.com/?v=Kanithan").content
soup = BeautifulSoup(html)
urls = [ifr["src"] for ifr in soup.select("table:nth-of-type(2)")[0].select("iframe")]
这会给你四个:
['http://www.playhd.video/embed.php?vid=621',
'http://mersalaayitten.com/embed/3752',
'http://www.playhd.video/embed.php?vid=584',
'http://googleplay.tv/videos/kanithan?iframe=true']
使用lxml 和 xpath 甚至更容易:
import requests
html = requests.get("http://kathaltamil.com/?v=Kanithan").content
from lxml.etree import fromstring, HTMLParser
xml = fromstring(html, HTMLParser())
print(xml.xpath("//table[2]//iframe/@src"))
这给了你同样的效果:
['http://www.playhd.video/embed.php?vid=621',
'http://mersalaayitten.com/embed/3752',
'http://www.playhd.video/embed.php?vid=584',
'http://googleplay.tv/videos/kanithan?iframe=true']
无论您选择什么都将是比您的正则表达式更好的选择。