使用Python提取HTML链接
我试图使用Python提取给定一组站点的iframe src。例如,我的输入是A.com、B.com、C.com,如果这些网站都有链接到D.com、E.com、F.com的iframe(如果网站没有iframe,则为“无”),那么我希望输出的形式如下:使用Python提取HTML链接,python,html,arrays,list,iframe,Python,Html,Arrays,List,Iframe,我试图使用Python提取给定一组站点的iframe src。例如,我的输入是A.com、B.com、C.com,如果这些网站都有链接到D.com、E.com、F.com的iframe(如果网站没有iframe,则为“无”),那么我希望输出的形式如下: Site Iframe Src A.com D.com B.com E.com C.com F.com 目前,我有这样的想法: from collections import defaultdict import url
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"]
从集合导入defaultdict
导入urllib2
进口稀土
def打印链接(网站):
计数器=0
regexp\u link=regexp\u link=r''”您不需要使用regex重新发明轮子,有一些很棒的python包可以为您做到这一点,可能是最著名的BeautifulSoup
使用pip安装和,然后尝试此操作
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']