我有一段代码(可能非常低效,但这是另一个故事),它从博客的HTML代码中提取URL。我将HTML保存在.csv文件中,然后将其放入Python中,并运行正则表达式以获取URL。以下是代码:
import csv, re # required imports
infile = open('Book1.csv', 'rt') # open the csv file
reader = csv.reader(infile) # read the csv file
strings = [] # initialize a list to read the rows into
for row in reader: # loop over all the rows in the csv file
strings += row # put them into the list
link_list = [] # initialize list that all the links will be put in
for i in strings: # loop over the list to access each string for regex (can't regex on lists)
links = re.search(r'((https?|ftp)://|www\.)[^\s/$.?#].[^\s]*', i) # regex to find the links
if links != None: # if it finds a link..
link_list.append(links) # put it into the list!
for link in link_list: # iterate the links over a loop so we can have them in a nice column format
print(link)
这个代码是有效的,不过当我打印结果时,它的格式是:
<_sre.SRE_Match object; span=(49, 80), match='http://buy.tableausoftware.com"'>
<_sre.SRE_Match object; span=(29, 115), match='https://c.velaro.com/visitor/requestchat.aspx?sit>
<_sre.SRE_Match object; span=(34, 117), match='https://www.tableau.com/about/blog/2015/6/become->
<_sre.SRE_Match object; span=(32, 115), match='https://www.tableau.com/about/blog/2015/6/become->
<_sre.SRE_Match object; span=(76, 166), match='https://www.tableau.com/about/blog/2015/6/become->
<_sre.SRE_Match object; span=(9, 34), match='http://twitter.com/share"'>
有没有办法让我只从其他无关的内容中提取链接?这是正则表达式搜索的一部分吗?谢谢!