鱼C论坛

 找回密码
 立即注册
查看: 1836|回复: 6

[已解决]python爬取诗词网乱码

[复制链接]
发表于 2021-7-18 19:25:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
感谢大佬,不知道哪里与问题了。。请求帮助

代码如下:
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
page_text = requests.get(url=url,headers=headers).text
fp = open('./sanguo.txt','w',encoding='utf-8')
soup = BeautifulSoup(page_text,'lxml')
a_list = soup.select('.book-mulu > ul > li > a')
for a in a_list:
    title = a.string
    detail_url = 'https://www.shicimingju.com'+a['href']
    page_text_detail = requests.get(url=detail_url,headers=headers).text
    soup = BeautifulSoup(page_text_detail,'lxml')
    div_tag = soup.find('div',class_="chapter_content")
    content = div_tag.text
    fp.write(title+':'+content+'\n')
    print(title,'保存成功!!!')
fp.close()
最佳答案
2021-7-18 19:58:45
import requests
from bs4 import BeautifulSoup

url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
# 以下为修改的代码 该网页是用utf-8 来编码,所以要进行编码处理,这是第一处
req = requests.get(url=url, headers=headers)
req.encoding = ("utf-8")
page_text = req.text

fp = open('./sanguo.txt', 'w', encoding='utf-8')
soup = BeautifulSoup(page_text, 'lxml')
a_list = soup.select('.book-mulu > ul > li > a')
for a in a_list:
    title = a.string
    detail_url = 'https://www.shicimingju.com'+a['href']
    # 这是第二处
    req2 = requests.get(url=detail_url, headers=headers)
    req2.encoding = ("utf-8")
    page_text_detail = req2.text
    soup = BeautifulSoup(page_text_detail, 'lxml')
    div_tag = soup.find('div', class_="chapter_content")
    content = div_tag.text
    fp.write(title+':'+content+'\n')
    print(title, '保存成功!!!')
fp.close()
乱码一般都是你的编码有问题,参考这篇文章
https://blog.csdn.net/wyb199026/ ... 1018.2226.3001.4187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-18 19:28:40 | 显示全部楼层

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-18 19:33:01 | 显示全部楼层
乱码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-18 19:58:45 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +1 鱼币

import requests
from bs4 import BeautifulSoup

url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
# 以下为修改的代码 该网页是用utf-8 来编码,所以要进行编码处理,这是第一处
req = requests.get(url=url, headers=headers)
req.encoding = ("utf-8")
page_text = req.text

fp = open('./sanguo.txt', 'w', encoding='utf-8')
soup = BeautifulSoup(page_text, 'lxml')
a_list = soup.select('.book-mulu > ul > li > a')
for a in a_list:
    title = a.string
    detail_url = 'https://www.shicimingju.com'+a['href']
    # 这是第二处
    req2 = requests.get(url=detail_url, headers=headers)
    req2.encoding = ("utf-8")
    page_text_detail = req2.text
    soup = BeautifulSoup(page_text_detail, 'lxml')
    div_tag = soup.find('div', class_="chapter_content")
    content = div_tag.text
    fp.write(title+':'+content+'\n')
    print(title, '保存成功!!!')
fp.close()
乱码一般都是你的编码有问题,参考这篇文章
https://blog.csdn.net/wyb199026/ ... 1018.2226.3001.4187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 20:02:00 | 显示全部楼层
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
page_text = requests.get(url=url, headers=headers, verify=False)
page_text.encoding = page_text.apparent_encoding
page_text = page_text.text #以上两行代码为添加的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 20:57:54 | 显示全部楼层
大马强 发表于 2021-7-18 19:58
乱码一般都是你的编码有问题,参考这篇文章
https://blog.csdn.net/wyb199026/article/details/52562538 ...

感谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 20:59:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-4 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表