小说网站总榜上的小说你都看过吗?爬取总推荐排行榜所有小说

python

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

 

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

一、相关环境配置

  • python 3.6
  • pycharm
  • requests
  • parsel

相关模块 pip 安装即可

 

二、使用步骤

1.引入库

代码如下(示例):

import requests

import parsel

 

2.获取网页数据

代码如下(示例):

url = "https://www.tianyabook.com/top/allvote/"

headers = {

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"

}

response = requests.get(url=url, headers=headers)

response.encoding = response.apparent_encoding


运行返回结果:

 

3.解析数据

代码如下(示例):

selector = parsel.Selector(response.text)

urls = selector.css("table tr td:nth-child(1) a::attr(href)").getall()

titles = selector.css("table tr td:nth-child(1) a::attr(title)").getall()

data = zip(urls, titles)

for i in data:

book_id = i[0].replace(".html", "").split("/")[-1]

title = i[1]

print(book_id, title)

 

运行返回结果:

 

4.保存数据

def download(title, book_id):

filename = "D:pythondemo电子书下载小说" + title + ".txt"

download_url = "http://www.tianyabook.com/modules/article/txtarticle.php?id={}".format(book_id)

response_2 = requests.get(url=download_url, headers=headers)

with open(filename, mode="a", encoding="utf-8") as f:

f.write(response_2.text)

 

 

总结

提示:这里对文章进行总结:
以上就是全部的内容,本文仅仅简单爬取小说网站,这里是直接访问小说下载的地址。

以上文章来源于CSND,作者 嗨学编程

以上是 小说网站总榜上的小说你都看过吗?爬取总推荐排行榜所有小说 的全部内容, 来源链接: utcz.com/z/530968.html

回到顶部