如何计算正则表达式中的项目总数



所以我写了一个程序来抓取Reg-Ex声明、电子邮件、电话号码和图像中的项目。我刚开始学习python不久。

我用来抓取网站的代码是:

def main():
    url = "URL in here!"
    webpage = urllib2.urlopen(url)
    content = webpage.read()
    f = open('CSN08115-TestPage.txt', 'w')
    f.write(content)
    f.close()
    print content
    print GetLink()
def GetLink():
    with open('CSN08115-TestPage.txt') as f: 
        for line in f: 
            c = re.findall(r'ashref="/?(.*)">', line)
            #Code to find total number of Lines of c
            if c:
                print c, 'Total number of emails: 6' #Output should adjust to different websites
if __name__ == "__main__":
main()

我的问题是如何计算RegEx语句的输出总数

我尝试过使用print c, len(c),但它只在每个输出旁边输出一个1!共有6封电子邮件。我的想法是,c=re.findall为在c中找到的每封电子邮件创建一个列表,从而为每封电子邮件提供1的结果?

如果没有看到输入,我不可能是肯定的,但我怀疑你应该对整个页面内容调用re.findall,而不是一次只调用一行:

   ...
   content = webpage.read()
   ...
   c = re.findall(r'ashref="/?(.*)">', content)
   number_of_items = len(c)

最新更新