计算一个单词的出现次数

2024-04-26 11:43:41 发布

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

我是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) 

我必须找到出现在每个链接和打印它!!你知道吗


Tags: thecom网站链接counttagslinkfind
2条回答

你可以用str.计数,有关详细信息,请查看here

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) 

相关问题 更多 >