鱼C论坛

 找回密码
 立即注册
查看: 1605|回复: 9

[已解决]爬取正文为空!!

[复制链接]
发表于 2019-8-26 16:10:56 | 显示全部楼层 |阅读模式

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

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

x
看了一段时间书,尝试着也来爬网络小说,

小说地址是:https://www.zhuishubang.com/131036/54103552.html

能把首页的标题都爬取到,正文都爬取不到。是“获取章节”里的那个地方出错了?

  1. from bs4 import BeautifulSoup
  2. import requests, sys

  3. class download(object):
  4.     def __init__(self):
  5.         self.server_url = 'http://www.zhuishubang.com'
  6.         self.target_url = 'http://www.zhuishubang.com/131036/'
  7.         self.names = []
  8.         self.urls = []
  9.         self.nums = 0

  10.     """
  11.     获取下载的链接
  12.     获取目录
  13.     """

  14.     def download_url(self):
  15.         req = requests.get(url=self.target_url)
  16.         html = req.text.encode("latin1").decode("gbk")
  17.         bf = BeautifulSoup(html, 'lxml')
  18.         texts = bf.find_all('div', 'chapterCon')
  19.         bf_a = BeautifulSoup(str(texts), 'lxml')
  20.         a = bf_a.find_all('a')
  21.         self.nums = len(a)
  22.         for i in a:
  23.             self.names.append(i.string)
  24.             self.urls.append(self.server_url + i.get('href'))

  25.     """
  26.     获取每一章节的内容
  27.     """

  28.     def download_content(self, target_url):
  29.         req = requests.get(url=target_url)
  30.         html = req.text.encode("latin1").decode("gbk")
  31.         bf = BeautifulSoup(html, 'lxml')
  32.         texts = bf.find_all('div', class_='articleCon')
  33.         bf_div = BeautifulSoup(str(texts), 'lxml')
  34.         div = bf_div.find_all('div')
  35.         txt = ''
  36.         for i in div:
  37.             if i.string is not None:
  38.                 txt = txt + i.string + '\n\n'
  39.         return txt

  40.     def writer(self, name, path, text):
  41.         write_flag = True
  42.         with open(path, 'a', encoding='utf-8') as f:
  43.             f.write(name + '\n')
  44.             f.writelines(text)
  45.             f.write('\n\n')


  46. if __name__ == '__main__':
  47.     dl = download()
  48.     dl.download_url()
  49.     print("开始下载")
  50.     for i in range(dl.nums):
  51.         dl.writer(dl.names[i], '全球制造.txt', dl.download_content(dl.urls[i]))
  52.         sys.stdout.write("已下载:%.3f%%" % float(i / dl.nums) + '\r')
  53.         sys.stdout.flush
  54.     print('已下载完成')
复制代码
最佳答案
2019-8-27 14:57:32
好像找到问题原因了:
  1. for i in div:
  2.             if i.string is not None:   ---这一句 i.string始终都是None
  3.                 txt = txt + i.string + '\n\n'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 17:02:40 | 显示全部楼层
  1. headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"}
  2.         req = requests.get(url=self.target_url,headers=headers)
  3. texts = bf.find_all('div',attrs={'class':'chapterList'})
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 09:02:32 | 显示全部楼层
同意楼上
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 14:57:32 | 显示全部楼层    本楼为最佳答案   
好像找到问题原因了:
  1. for i in div:
  2.             if i.string is not None:   ---这一句 i.string始终都是None
  3.                 txt = txt + i.string + '\n\n'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 14:58:49 | 显示全部楼层
请问 i.string  是个什么用法?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 15:21:57 | 显示全部楼层
把上面那一句改成这样,可以写一点章节内容进去:
  1. if str(i) is not None:
  2.                 txt = txt + str(i) + '\n\n'
复制代码


i.string   ---这个是什么用法呢?能帮忙解释下吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-27 15:49:47 | 显示全部楼层
Jery_wang09 发表于 2019-8-27 15:21
把上面那一句改成这样,可以写一点章节内容进去:

防止有空值,所以想把空值不抓取。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 16:27:35 | 显示全部楼层
seaman_w 发表于 2019-8-27 15:49
防止有空值,所以想把空值不抓取。

i.string  ---这个用法怎么来的?有什么出处吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-27 16:31:18 | 显示全部楼层
Jery_wang09 发表于 2019-8-27 16:27
i.string  ---这个用法怎么来的?有什么出处吗

你可以查看bs4文档
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 09:54:39 | 显示全部楼层
塔利班 发表于 2019-8-27 16:31
你可以查看bs4文档

看到了,多谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 14:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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