鱼C论坛

 找回密码
 立即注册
查看: 2967|回复: 3

[已解决]python爬虫,成功安装bs4,执行程序却保错:import requests from bs4 import Beaut...

[复制链接]
发表于 2022-7-29 15:34:54 | 显示全部楼层 |阅读模式

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

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

x

  1. #爬取三国演义小说
  2. import requests
  3. from bs4 import BeautifulSoup      #程序报错:ModuleNotFoundError: No module named 'html.entities'; 'html' is not a package, 这是怎么回事,该怎么解决

  4. url='http://sanguo.5000yan.com/'
  5. headers ={'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36'}

  6. response = requests.get(url=url,headers=headers)
  7. response.encoding='utf-8'
  8. page_text=response.text

  9. soup = BeautifulSoup(page_text,'lxml')
  10. #print(soup)
  11. li_list = soup.select(".sidamingzhu-list-mulu>ul>li")
  12. #print(li_list)
  13. fp =open('./sanguo.txt','w',encoding='utf-8')#放到循环上面,保证文件被打开一次,这样所有章节都会被追加写入
  14. for li in li_list:
  15.     #解析标题和正文
  16.     title =li.a.string
  17.     detail_url =li.a['href']#一个IP就是对应一个网页或者一张图片
  18.     response =requests.get(url=detail_url,headers=headers)
  19.     response.encoding='utf-8'
  20.     detail_url_text = response.text
  21.     #解析正文
  22.     detail_soup =BeautifulSoup(detail_url_text,'lxml')
  23.     detail_tag =detail_soup.find('div',class_="grap")
  24.     #print(detail_tag)
  25.     content =detail_tag.text
  26.     #fp =open('./sanguo.txt','w',encoding='utf-8')  #放到此处每循环一次,文件就会被打开一次,这样只有最后一次写入被保存
  27.    
  28.    
  29.     fp.write(title +':::::::'+content+'\n')
  30.     print(title,'下载成功')

  31. url='http://sanguo.5000yan.com/'
  32. headers ={'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36'}

  33. response = requests.get(url=url,headers=headers)
  34. response.encoding='utf-8'
  35. page_text=response.text

  36. soup = BeautifulSoup(page_text,'lxml')
  37. #print(soup)
  38. li_list = soup.select(".sidamingzhu-list-mulu>ul>li")
  39. #print(li_list)
  40. fp =open('./sanguo.txt','w',encoding='utf-8')#放到循环上面,保证文件被打开一次,这样所有章节都会被追加写入
  41. for li in li_list:
  42.     #解析标题和正文
  43.     title =li.a.string
  44.     detail_url =li.a['href']#一个IP就是对应一个网页或者一张图片
  45.     response =requests.get(url=detail_url,headers=headers)
  46.     response.encoding='utf-8'
  47.     detail_url_text = response.text
  48.     #解析正文
  49.     detail_soup =BeautifulSoup(detail_url_text,'lxml')
  50.     detail_tag =detail_soup.find('div',class_="grap")
  51.     #print(detail_tag)
  52.     content =detail_tag.text
  53.     #fp =open('./sanguo.txt','w',encoding='utf-8')  #放到此处每循环一次,文件就会被打开一次,这样只有最后一次写入被保存
  54.    
  55.    
  56.     fp.write(title +':::::::'+content+'\n')
  57.     print(title,'下载成功')
复制代码
最佳答案
2022-7-29 16:29:33
本帖最后由 临时号 于 2022-7-29 16:31 编辑

你的py文件存放路径下可能有一个文件的名字将html.py,把它换一个名字
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-29 16:29:33 | 显示全部楼层    本楼为最佳答案   
本帖最后由 临时号 于 2022-7-29 16:31 编辑

你的py文件存放路径下可能有一个文件的名字将html.py,把它换一个名字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-29 17:02:44 | 显示全部楼层
临时号 发表于 2022-7-29 16:29
你的py文件存放路径下可能有一个文件的名字将html.py,把它换一个名字

非常感谢,困惑了我一下午
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-29 17:05:20 | 显示全部楼层
huyanmin 发表于 2022-7-29 17:02
非常感谢,困惑了我一下午

我的html.py和谁冲突了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 07:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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