shtml 包含 乱码问题 解决方案

配置SSI字符集
本文介绍如何在Tomcat的web.xml文件中为SSI servlet设置字符集参数outputEncoding为GBK,以改变默认的UTF-8编码。

终于搞定了。
即在$CATALINA_BASE/conf/web.xml中,找到ssi的servlet,在其中加入一个参数,如下所示:
         <init-param>
          <param-name>outputEncoding</param-name>
          <param-value>GBK</param-value>
        </init-param>
如果不加该参数,则字符输出默认按utf-8处理。

import requests from bs4 import BeautifulSoup import pandas as pd import time import random # 请求头设置 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'Referer': 'https://www.gov.cn/' } def fetch_data(url): """抓取并解析页面数据""" try: response = requests.get(url, headers=headers) response.encoding = 'utf-8' soup = BeautifulSoup(response.text, 'lxml') data_list = [] for item in soup.select('div.resultItem'): # 提取基础信息 title = item.select_one('h3.title').get_text(strip=True) pubtime = item.select_one('div.pubtimestr').get('data-time') detail_url = item.select_one('a')['href'] # 抓取详情页文本内容 detail_text = fetch_detail(detail_url) data_list.append({ '发布时间': pubtime, '标题': title, '文本内容': detail_text }) # 随机延迟1-3秒 time.sleep(random.uniform(1, 3)) return data_list except Exception as e: print(f"请求失败: {str(e)}") return [] def fetch_detail(url): """抓取详情页正文内容""" try: response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'lxml') content = ' '.join([p.get_text(strip=True) for p in soup.select('div.pages_content p')]) return content except: return "内容获取失败" def save_to_excel(data, filename): """保存数据到Excel""" df = pd.DataFrame(data) df.to_excel(filename, index=False, engine='openpyxl') print(f"数据已保存至{filename}") if __name__ == "__main__": target_url = "https://sousuo.www.gov.cn/sousuo/search.shtml?code=17da70961a7&dataTypeId=107&searchWord=%E7%BB%BF%E8%89%B2" data = fetch_data(target_url) save_to_excel(data, '政府数据.xlsx') excel里面没有内容 如何解决 最后写一个完整的
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值