趣味学python(007)

python应用——爬取起点小说

小编最近找到了爬取小说的内容,思来想去,照输之后(本来想爬取一个小说的内容)却发现爬取不到章链接和章明。????

无奈。。。

import requests
from lxml import etree
import os

# 定义一个类(使代码更加标准化)
class Spider(object):
def index_request(self):
# 1.请求起点网拿到对应的HTML数据,抽取小说名,小说链接,创建文件夹
response = requests.get("https://www.qidian.com/all?")
# 整理成文档对象
html = etree.HTML(response.text)
Big_src = html.xpath(’//li[@data-rid=“1”]/div[@class=“book-mid-info”]/h4/a/@href’)
Big_tit = html.xpath(’//li[@data-rid=“1”]/div[@class=“book-mid-info”]/h4/a/text()’)
for Bigtit, Bigsrc in zip(Big_tit, Big_src):
if os.path.exists(Bigtit) == False:
os.mkdir(Bigtit)
self.detail_request(Bigtit, Bigsrc)

def detail_request(self,Bigtit, Bigsrc):

# 请求目录拿到HTML数据,抽取章名,章链接
response = requests.get(“https:” + Bigsrc)
# 整理成文档对象
html = etree.HTML(response.text)
littit_list = html.xpath(’//div[@class=“volume”]/ul/li/a/text()’)
litsrc_list = html.xpath(’//div[@class=“volume”]/ul/li/a/@href’)
print(littit_list,litsrc_list)
for littit, litsrc in zip(littit_list, litsrc_list):
self.content_request(littit, litsrc, Bigtit)

def content_request(self, littit, litsrc, Bigtit):
# 请求文章拿到数据,抽取文章内容,保存数据
response = requests.get(“https:” + litsrc)
html = etree.HTML(response.text)
content = “\n”.join(html.xpath(’//div[@class=“read-content j_readContent”]/p/text()’))
filename = Bigtit + “\” + littit + “.txt”
print(“正在保存文件 %s” %filename)
with open(filename, “w”) as f:
f.write(content)
最后的代码实现:
spider = Spider()
spider.index_request()

但是小编在输入之后,却没有访问到章链接和章明,怎么办呢???????

哭哭了。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值