简体   繁体   中英

Search for a specific word in html file in Python

I want to count how many times a specific word can be found in a html file. The html file is downloaded from an url:

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

Since there are no specific lines in a html file I am having trouble going through the file in search for the specific word... Is there an easy way to do this?

试试下面的代码:

num_occ = source.count("your_specific_word")

So if are looking for a web scraping tool then go with beautiful soup or scrapy

Else you could just calculate the count by the number of occurrences in the text by simply using count

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

Python string count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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