使用Python提取HTML链接

2024-04-25 02:16:31 发布

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

我正在尝试使用Python提取给定一组站点的iframe src。例如,我的输入是A.com、B.com、C.com,如果这些站点都有链接到D.com、E.com、F.com的iframe(如果站点没有iframe,则为“None”),那么我希望输出的形式如下:

Site    Iframe Src
A.com    D.com
B.com    E.com
C.com    F.com

目前,我有这样的想法:

from collections import defaultdict
import urllib2
import re

 def PrintLinks(website):
 counter = 0
 regexp_link= regexp_link = r'''<frame src =((http|ftp)s?://.*?)'''
 pattern = re.compile(regexp_link)
 links = [None]*len(website)
 for x in website:
     html_page = urllib2.urlopen(website[counter])
     html = html_page.read()
     links[counter] = re.findall(pattern,html)
     counter += 1
 return links

def main():
 website=["A.com","B.com","C.com"]

这是最好的方法吗?我如何得到我想要的格式的输出?谢谢!你知道吗


Tags: importresrccomnone站点defhtml
1条回答
网友
1楼 · 发布于 2024-04-25 02:16:31

您不需要使用regex重新发明轮子,有一些很棒的python包可以为您做到这一点,成为最著名的BeautifulSoup。你知道吗

用pip安装BeautifulSouphttplib2,然后尝试以下操作


import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer

sites=['http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com']
http = httplib2.Http()

for site in sites:
    status, response = http.request(site)
    for iframe in BeautifulSoup(response, parseOnlyThese=SoupStrainer('iframe')):
        print site + ' ' + iframe['src']

相关问题 更多 >