【问题标题】:Search the frequency of words in the sub pages of a webpage using Python使用Python搜索网页子页面中单词的频率
【发布时间】:2017-05-24 15:42:09
【问题描述】:

当我被困在如何抓取网页中的每个链接(页面或子页面)并找到任何单词的频率时,我寻求帮助。我用了漂亮的汤 刮,但我不认为我做对了。例如:我需要进入 Service now 官方页面 > 解决方案 > 查看所有解决方案。并在查看所有解决方案下的所有链接/子页面中找到“智能”的频率。 任何帮助将不胜感激。 谢谢你:)

我的代码

import requests
from bs4 import BeautifulSoup

url = "https://www.servicenow.com/solutions-by-category.html"
serviceNow_r = requests.get(url)
sNow_soup = BeautifulSoup(serviceNow_r.text, 'html.parser')

print(sNow_soup.find_all('href',{'class':'cta-list component'}))


for name in sNow_soup.find_all('href',{'class':'cta-list component'}):
    print(name.text)

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    这是访问页面中每个链接的 href 属性所需要的。

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://www.servicenow.com/solutions-by-category.html"
    serviceNow_r = requests.get(url)
    sNow_soup = BeautifulSoup(serviceNow_r.text, 'html.parser')
    
    for anchor in sNow_soup.find_all('a', href=True):
        print(anchor['href'])
    

    【讨论】:

      【解决方案2】:

      您正在搜索href 标记。这是错误的!

      您应该搜索a 标记,然后获取href 属性。这是链接页面的网址。

      【讨论】:

        猜你喜欢
        • 2018-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        相关资源
        最近更新 更多