繁体   English   中英

从大型文本文件中打印多个网址

[英]Print multiple urls from large text file

我正在尝试使用正则表达式从一个大文本文件中查找并打印所有.com网址。 由于大约有40个不同的网址,我想知道是否有一种方法可以搜索它们而不用一个一个地进行搜索。

我使用的代码为xxxx.com,但开头缺少https //:www。 谁能告诉我我如何得到全部结果? 先感谢您!

import re 
url = len(".com") 
re = re.compile(r'\w*.com\b', url) 
for line in open("report.txt"): 
    for url in re.findall(line): 
        print url

这似乎可行:

#!/usr/local/cpython-2.7/bin/python

import re

def main():
    regex = re.compile(r'https?://[^ \t]*.com\b', re.MULTILINE | re.DOTALL)

    with open('logs.txt', 'r') as file_:
        text = file_.read()

    for url in regex.findall(text):
        print(url)

main()

高温超导

#!/usr/bin/python

import urllib
import urlparse
import re
import requests

#
# A class for dealing with links 
#

class linkGrabber:

  linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')

  #
  # Remove White space and hash tags 
  #

  def clean(self,link):
    link = re.sub(' ','',link)
    link = re.sub("#",'',link)
    return link

def depth(self,link):  
    return len(urlparse.urlparse(url).path.split("/")) -1

  def isAbsolute(self,link):
    return len(urlparse.urlparse(link).netloc) > 0

  def isRelative(self,link):
    return len(urlparse.urlparse(link).netloc) < 1

  def grab(self,markup,*args):
    links = self.linkregex.findall(markup)
    relative = []
    absolute = []
    for this in links:
      #this = urlparse.urlparse(this)
      if self.isAbsolute(this) == True:
        absolute.append(this)
      elif  self.isAbsolute(this) == False:
        relative.append(this)
    if len(args) <=0:
      return relative + absolute
    elif "abs" in args:
      return absolute
    else:
      return relative

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM