鱼C论坛

 找回密码
 立即注册
查看: 2164|回复: 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
  1. import requests
  2. from bs4 import BeautifulSoup

  3. url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
  4. headers = {
  5.     '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'}
  6. # 以下为修改的代码 该网页是用utf-8 来编码,所以要进行编码处理,这是第一处
  7. req = requests.get(url=url, headers=headers)
  8. req.encoding = ("utf-8")
  9. page_text = req.text

  10. fp = open('./sanguo.txt', 'w', encoding='utf-8')
  11. soup = BeautifulSoup(page_text, 'lxml')
  12. a_list = soup.select('.book-mulu > ul > li > a')
  13. for a in a_list:
  14.     title = a.string
  15.     detail_url = 'https://www.shicimingju.com'+a['href']
  16.     # 这是第二处
  17.     req2 = requests.get(url=detail_url, headers=headers)
  18.     req2.encoding = ("utf-8")
  19.     page_text_detail = req2.text
  20.     soup = BeautifulSoup(page_text_detail, 'lxml')
  21.     div_tag = soup.find('div', class_="chapter_content")
  22.     content = div_tag.text
  23.     fp.write(title+':'+content+'\n')
  24.     print(title, '保存成功!!!')
  25. fp.close()
复制代码

乱码一般都是你的编码有问题,参考这篇文章
https://blog.csdn.net/wyb199026/ ... 1018.2226.3001.4187
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-18 19:33:01 | 显示全部楼层
乱码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

回帖奖励 +1 鱼币

  1. import requests
  2. from bs4 import BeautifulSoup

  3. url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
  4. headers = {
  5.     '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'}
  6. # 以下为修改的代码 该网页是用utf-8 来编码,所以要进行编码处理,这是第一处
  7. req = requests.get(url=url, headers=headers)
  8. req.encoding = ("utf-8")
  9. page_text = req.text

  10. fp = open('./sanguo.txt', 'w', encoding='utf-8')
  11. soup = BeautifulSoup(page_text, 'lxml')
  12. a_list = soup.select('.book-mulu > ul > li > a')
  13. for a in a_list:
  14.     title = a.string
  15.     detail_url = 'https://www.shicimingju.com'+a['href']
  16.     # 这是第二处
  17.     req2 = requests.get(url=detail_url, headers=headers)
  18.     req2.encoding = ("utf-8")
  19.     page_text_detail = req2.text
  20.     soup = BeautifulSoup(page_text_detail, 'lxml')
  21.     div_tag = soup.find('div', class_="chapter_content")
  22.     content = div_tag.text
  23.     fp.write(title+':'+content+'\n')
  24.     print(title, '保存成功!!!')
  25. fp.close()
复制代码

乱码一般都是你的编码有问题,参考这篇文章
https://blog.csdn.net/wyb199026/ ... 1018.2226.3001.4187
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 20:02:00 | 显示全部楼层
  1. headers = {
  2.     "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"
  3. }
  4. page_text = requests.get(url=url, headers=headers, verify=False)
  5. page_text.encoding = page_text.apparent_encoding
  6. page_text = page_text.text #以上两行代码为添加的
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

感谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 20:59:50 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 23:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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