繁体   English   中英

数词的出现

[英]Count The occurence of a word

我是Python的新手,它利用python中的请求。 我被要求在xyz.com上进行登录,这是我能够做到的。 并提取所有与讨论有关的表格内容。 在每个链接中,我需要查找“ The”单词的出现。 我应该如何进行?我的代码如下

tags=content2.findAll("td",{'class':'topic starter'})
for i in tags:
    thread_link=i.find('a').get('href')
    print(thread_link)
    result3=session.post(thread_link)
    content3=bs4.Beautifulsoup(result3.text,'html.parser')
    tag3=content3.find("the",count+1) 
    print(count) 

我必须在每个链接中找到的出现并进行打印!!

您可以使用str.count, 在此处查看更多详细信息

for i in tags:
    thread_link=i.find('a').get('href')
    result3=session.post(thread_link)
    count = result3.text.count("the")
    print(count) 

您没有正确执行此操作。 您的tag3将找到the标签。 代码也有点混乱。 您可以将正则表达式用于您的事业。 在这里,我们将搜索the结果中的文本。

import re
for i in tags:
    thread_link=i.find('a').get('href')
    print(thread_link)
    result3=session.post(thread_link)
    count=len(re.findall('\sthe\s',result3.text))
    print(count) 

暂无
暂无

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

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