鱼C论坛

 找回密码
 立即注册
查看: 2608|回复: 5

[已解决]爬虫爬小说

[复制链接]
发表于 2020-8-21 23:57:21 | 显示全部楼层 |阅读模式

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

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

x
  1. import requests
  2. import re

  3. #name = input('请输入想要查找的小说的名字:')

  4. params = {
  5.     'q':'无敌真寂寞'
  6. }

  7. headers = {
  8.     'Accept-Language': 'zh-CN',
  9.     'Cache-Control': 'no-cache',
  10.     'Connection': 'Keep-Alive',
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
  12.     }
  13. url = 'https://www.biduo.cc/search.php?'
  14. res = requests.get(url=url,params=params,headers=headers)
  15. html = res.text

  16. # regular = re.compile('')
  17. # m = regular.findall(html)
  18. print(m)
复制代码

想爬个小说,html就是查找那部小说的连接,但是找小说章节连接的正则表达式写不出来,有大佬能指点一下吗
最佳答案
2020-8-22 02:51:18
这样能爬到搜索榜一的小说详情页,看看有没有启发
  1. import requests
  2. import bs4
  3. import os

  4. url = f'https://www.biduo.cc/search.php?q={input("请输入小说名:")}'

  5. headers = {
  6.     'Accept-Language': 'zh-CN',
  7.     'Cache-Control': 'no-cache',
  8.     'Connection': 'Keep-Alive',
  9.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
  10.     }

  11. res = requests.get(url,headers=headers)
  12. s = bs4.BeautifulSoup(res.text,features="lxml")

  13. xiaoshuo = s.find("h3",class_="result-item-title result-game-item-title") #找到 第一个 标签(即为搜索榜一) 找全部可用s.find_all

  14. url_ = xiaoshuo.find("a").get ('href')# 在获得的标签中 继续找到 a 标签,并get 到 href 属性
  15. print("https://www.biduo.cc" + url_) #加前缀
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-22 02:51:18 | 显示全部楼层    本楼为最佳答案   
这样能爬到搜索榜一的小说详情页,看看有没有启发
  1. import requests
  2. import bs4
  3. import os

  4. url = f'https://www.biduo.cc/search.php?q={input("请输入小说名:")}'

  5. headers = {
  6.     'Accept-Language': 'zh-CN',
  7.     'Cache-Control': 'no-cache',
  8.     'Connection': 'Keep-Alive',
  9.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
  10.     }

  11. res = requests.get(url,headers=headers)
  12. s = bs4.BeautifulSoup(res.text,features="lxml")

  13. xiaoshuo = s.find("h3",class_="result-item-title result-game-item-title") #找到 第一个 标签(即为搜索榜一) 找全部可用s.find_all

  14. url_ = xiaoshuo.find("a").get ('href')# 在获得的标签中 继续找到 a 标签,并get 到 href 属性
  15. print("https://www.biduo.cc" + url_) #加前缀
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-22 02:52:26 | 显示全部楼层
Wei_Jian_Xian 发表于 2020-8-22 02:51
这样能爬到搜索榜一的小说详情页,看看有没有启发

导入的os库没用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-22 07:51:34 | 显示全部楼层
  1. import requests
  2. import re

  3. #name = input('请输入想要查找的小说的名字:')

  4. params = {
  5.     'q':'无敌真寂寞'
  6. }

  7. headers = {
  8.     'Accept-Language': 'zh-CN',
  9.     'Cache-Control': 'no-cache',
  10.     'Connection': 'Keep-Alive',
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
  12.     }
  13. url = 'https://www.biduo.cc/search.php?'
  14. res = requests.get(url=url,params=params,headers=headers)
  15. html = res.text

  16. regular = re.compile('cpos="title".*?class')
  17. m = regular.findall(html)
  18. print(m)
复制代码

在此基础上提取链接
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-22 08:16:52 | 显示全部楼层
Wei_Jian_Xian 发表于 2020-8-22 02:51
这样能爬到搜索榜一的小说详情页,看看有没有启发

我感受到了自己知识的贫瘠
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-22 08:17:38 | 显示全部楼层
1q23w31 发表于 2020-8-22 07:51
在此基础上提取链接

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 09:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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