【问题标题】:How to find a specific word in multiple webpages or urls and count it, using Python如何使用 Python 在多个网页或 url 中查找特定单词并计算它
【发布时间】:2019-08-06 05:45:31
【问题描述】:

下面是我的代码。请检查并纠正我。

import requests

from bs4 import BeautifulSoup

url = ["https://www.tensorflow.org/","https://www.tomordonez.com/"]

the_word = input()

r = requests.get(url, allow_redirects=False)

soup = BeautifulSoup(r.content, 'lxml')

words = soup.find(text=lambda text: text and the_word in text)

print(words)

count = len(words)

print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, the_word))

如何更改我的代码以解析多个 URL 并计算特定单词出现的次数?

【问题讨论】:

  • 你的问题是什么?为什么你的代码不起作用?什么是预期的输出?请详细填写您的问题。
  • 我想要通过多个 url 来计算特定单词的数量???该怎么做?

标签: python url web-scraping beautifulsoup


【解决方案1】:
import requests
from bs4 import BeautifulSoup

url_list = ["https://www.tensorflow.org/","https://www.tomordonez.com/"]

#the_word = input()
the_word = 'Python'

total_words = []
for url in url_list:
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content.lower(), 'lxml')
    words = soup.find_all(text=lambda text: text and the_word.lower() in text)
    count = len(words)
    words_list = [ ele.strip() for ele in words ]
    for word in words:
        total_words.append(word.strip())

    print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, the_word))
    print(words_list)


#print(total_words)
total_count = len(total_words)

输出:

Url: https://www.tensorflow.org/
contains 0 of word: Python
[]

Url: https://www.tomordonez.com/
contains 8 of word: Python
['web scraping with python', 'this is a tutorial on web scraping with python. learn to scrape websites with python and beautifulsoup.', 'python unit testing tutorial', 'this is a tutorial about unit testing in python.', 'pip install ssl module in python is not available', 'troubleshooting ssl module in python is not available', 'python context manager', 'a short tutorial about python context manager: "with" statement.']

【讨论】:

    【解决方案2】:

    您可以使用re 模块查找特定文本。

    import requests
    import re
    from bs4 import BeautifulSoup
    
    urls = ["https://www.tensorflow.org/","https://www.tomordonez.com/"]
    
    the_word ='Tableau'
    
    for url in urls:
     print(url)
     r = requests.get(url, allow_redirects=False)
     soup = BeautifulSoup(r.text, 'html.parser')
     words = soup.find_all(text=re.compile(the_word))
     print(len(words))
    

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      相关资源
      最近更新 更多