簡體   English   中英

在Python中的html文件中搜索特定單詞

[英]Search for a specific word in html file in Python

我想計算在html文件中可以找到一個特定單詞的次數。 html文件是從url下載的:

with urlopen(url) as response:
   source = response.read()

由於html文件中沒有特定的行,因此我在瀏覽文件中查找特定單詞時遇到了麻煩。是否有一種簡單的方法?

試試下面的代碼:

num_occ = source.count("your_specific_word")

因此,如果您正在尋找網頁抓取工具,則可以搭配漂亮的湯沙皮

否則,您只需要通過使用count即可通過文本中出現的次數來計算count

with urlopen(url) as response:
   source = response.read()
noOfOccurances = source.count(searchWord)

Python字符串計數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM