打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python实现网页敏感词批量查找​

本例要查找的网页保存在”D:\test.xlsx”文件中第一列,要查找的敏感词为“第一”“质量”“最”(可根据实际需要修改敏感词内容和数量),运行代码后查找结果保存在”D:\test.xlsx”文件中第二列,以下代码已经在Python27中调试通过:

#coding=utf-8

import os
import re
import requests
import string
from openpyxl import Workbook
from openpyxl import load_workbook
from requests import exceptions

def get_html(url):
headers = {
'User-Agent''Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\
AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36'
#模拟浏览器访问
try:
r = requests.get(url,headers = headers) #请求访问网站
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except requests.RequestException as e:
return ''


def rexcel(excelFile): #excel文件
list1 = []
if not os.path.exists(excelFile):
print("文件不存在")
return list1
wb = load_workbook(excelFile)
ws = wb.active
for in range(ws.max_row):
r = j + 1
list1.append(ws.cell(row=rcolumn=1).value)
return list1

def wexcel(excelFile,list2): #excel文件
if not os.path.exists(excelFile):
print("文件不存在")
return 0
wb = load_workbook(excelFile)
ws = wb.active
for row in range(len(list2)):
#print(row)
r = row + 1
ws.cell(row=rcolumn=2).value = list2[row]
wb.save(excelFile)
return 1



if __name__ == '__main__'#程序入口
excelFile = 'D:/test.xlsx'
list1=rexcel(excelFile)
#print(list1[2])
pattern = \
ur'([\u4e00-\u9fa5]{1,}(第一|质量|))' #敏感词之间用|分隔
re_compile = re.compile(pattern)

list2 = []
for in range(len(list1)):
content = ''
s1=list1[m]
if s1==None:
break

if s1[0:4]=='http':
webcontent = get_html(s1)
else:
continue
#print webcontent

for in re_compile.finditer(webcontent):
content = content + n.group() + ';'

list2.append(content)
#print(list2)

wexcel(excelFile,list2) 

运行后的”D:\test.xlsx”文件如下图:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python3 读取和写入excel xlsx文件
(ok)python3 如何将txt与excel 进行相互转换(.xls、.xlsx)
Python操作Excel之xlsx文件
Python 读写excel文件
使用Python处理excel表格(openpyxl)教程
Python办公自动化|10个方法,是时候对Excel下手了
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服