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

2024-10-02 18:17:07 发布

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

因此,我写了一个程序来刮一个网站的项目在注册前声明,电子邮件,电话号码和图像。我记得我刚开始学习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'a\shref="/?(.*)">', 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=关于芬德尔为在c中找到的每封电子邮件创建一个列表,依次给出每封电子邮件1的结果?你知道吗


Tags: ofintxturl网站maindefline
1条回答
网友
1楼 · 发布于 2024-10-02 18:17:07

没有看到输入,我不能肯定,但我想你应该打电话关于芬德尔在整个页面内容上,而不是一次在一行上:

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

相关问题 更多 >