本文主要是介绍爬取飞卢小说,并计算文本中词语出现的次数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
'''打开开发者工具方法:F12(键盘)/fn+f12/ctrl+shift+i'''
import requests
import parsel
import re
import osurl = 'https://b.faloo.com/724903.html'
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response_1 = requests.get(url=url,headers=headers)
response_1.encoding = response_1.apparent_encoding
select_1 = parsel.Selector(response_1.text)
href_list = select_1.css('.DivTd3 a::attr(href)').getall()
Title = select_1.css('#novelName::text').get()
print(Title)
print(href_list)
filename = f'{Title}\\'target_words = ['太玄圣地','顾长歌','而且','圣主','圣女']
# 定义一个函数,可以计算文本中词汇出现的个数
def get_counts(text,target_words):word_count = {}for word in target_words:word_count[word] = text.count(word)return word_countif not os.path.exists(filename):os.mkdir(filename)
for href in href_list:href = 'https:'+hrefheaders = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"}response = requests.get(url=href,headers=headers)response.encoding = response.apparent_encodingselector = parsel.Selector(response.text)title = re.findall('<h1>玄幻:我!天命大反派 (.*?)</h1>',response.text)[0]content = '\n'.join(selector.css('.noveContent p ::text').getall()) # 通过join方法,让列表转变为字符串result = get_counts(content,target_words) # 调用定义的计数函数print(result)with open(filename+title+'.txt',mode='w',encoding='utf-8') as f:f.write(content)break
结果展示:
{'太玄圣地': 9, '顾长歌': 1, '而且': 3, '圣主': 7, '圣女': 3}
计算字符串中词语出现的次数的函数定义:
def count_occurrences(text, target_words):# 将文本转换为小写,以便统计时不区分大小写text = text.lower()# 初始化一个空字典,用于存储每个目标词语的出现次数word_count = {}# 遍历目标词语列表,统计每个词语的出现次数for word in target_words:# 使用字符串的 count 方法统计词语出现的次数,并存储到字典中word_count[word] = text.count(word)return word_count# 统计目标词语 "老公" 和 "老婆" 在文本中的出现次数
text = "老公,我好想你,老婆,我也是啊,可是明天就要去见另一个老公了,我还没准备好呢老婆?"
target_words = ["老公", "老婆"]
result = count_occurrences(text, target_words)
print(result)
这篇关于爬取飞卢小说,并计算文本中词语出现的次数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!