鱼C论坛

 找回密码
 立即注册
查看: 1173|回复: 1

哪里错了,怎么弄,点不会,怎么爬

[复制链接]
发表于 2019-4-27 21:47:52 | 显示全部楼层 |阅读模式

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

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

x
  1. import requests
  2. from lxml import etree
  3. import os
  4.     #设计模式--面向对象
  5. class Spider(object):
  6.     #1.请求一级页面HTML源代码
  7.     def start_request(self):
  8.         response = requests.get("https://www.qidian.com/all")
  9.         html=etree.HTML(response.text)
  10.         Bigtit_list=html.xpath('//div[@class="book-mid-info"]/h4/a/text()')
  11.         Bigsrc_list=html.xpath('//div[@class="book-mid-info"]/h4/a/@href')
  12.         for Bigtit, Bigsrc in zip(Bigtit_list,Bigsrc_list):
  13.             if os.path.exists(Bigtit)==False:
  14.                 os.mkdir(Bigtit)
  15.             self.next_file(Bigtit,Bigsrc)
  16.     def next_file(self,Bigtit,Bigsrc):
  17.         response=requests.get("https:" + Bigsrc)
  18.         html=etree.HTML(response.text)
  19.         Littit_list=html.xpath('//ul[@class="cf"]/li/a/text')
  20.         Litsrc_list=html.xpath('//ul[@class="cf"]/li/a/@href')
  21.         for Littit,Litsrc in zip(Littit_list,Litsrc_list):
  22.             self.finally_file(Littit,Litsrc,Bigtit)
  23.         

  24.     def finally_file(self, Littit,Litsrc,Bigtit):
  25.         response=requests.get("https:"+Litsrc)
  26.         html=etree.HTML(response.text)
  27.         content = "\n".join(html.xpath('//div[@class="read-content j_readContent"]/p/text()'))
  28.         file_name=Bigtit+"\"+Littit+".txt"
  29.         print("正在保存文件:"+file_name)
  30.         with open(file_name,"w",encoding="utf-8")as f:
  31.             f.file_name
  32.         

  33. spider = Spider()
  34. spider.start_request()
复制代码


哪里错了,怎么弄,点不会,怎么爬
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-28 00:52:48 | 显示全部楼层
是要爬取起点中文网所有小说的名字么?
我使用requests模块和BeautifulSoup4模块
  1. # import re
  2. from bs4 import BeautifulSoup
  3. import requests

  4. def get_reqbs(url):
  5.     '''
  6.     获取目标网页的beautifulsoup
  7.     :param url:
  8.     :return:
  9.     '''
  10.     headers = {
  11.         'Referer': url,
  12.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
  13.     }
  14.     req = requests.get(url,headers=headers)
  15.     urlbs = BeautifulSoup(req.text,'lxml')
  16.     return urlbs

  17. pages = input('输入爬取的页数:')

  18. for page in range(1,int(pages)+1):
  19.     base_url = 'https://www.qidian.com/all?orderId=&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=0&page={}'.format(page)
  20.     soup = get_reqbs(base_url)
  21.     infos = soup.find_all('div',class_='book-mid-info')
  22.     novel_infos = {}
  23.     for info in infos:
  24.         novel_info = {'作者':info.find('a',class_='name').text,
  25.                        '类型': info.find('p',class_='author').text.split('|')[1],
  26.                       '简介':info.find('p',class_='intro').text.strip()
  27.                       }
  28.         novel_infos[info.h4.text] = novel_info
  29.     print(novel_infos)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 13:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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