import requests
from lxml import etree
book_list = []
base_url = 'http://www.biqugei.net/'
res = requests.get(base_url)
html = etree.HTML(res.text)
# # 本周热读推荐
h4s = html.xpath('//h4')
for h4 in h4s:
book = {}
book_name = h4.xpath('a/text()')[0]
book_url = h4.xpath('a/@href')[0]
author = h4.xpath('../small/text()')[0]
book_info = h4.xpath('../p/text()')[0]
book['name'] = book_name
book['url'] = base_url + book_url # 拼接完整url
book['author'] = author
book['info'] = book_info
book_list.append(book)
print(book_list)
# 分类
divs = html.xpath('//div[contains(@class, "panel") and contains(@class, "panel-default")]/ul')
for ul in divs:
lis = ul.xpath('li')
for li in lis:
book = {}
book_name = li.xpath('a/text()')[0]
book_url = li.xpath('a/@href')[0]
author = li.xpath('span/text()')[0]
book['name'] = book_name
book['url'] = base_url + book_url # 拼接完整url
book['author'] = author
book_list.append(book)
print(book_list)
# 最新入库
trs = html.xpath('//table/tbody')[0].xpath('tr')
for tr in trs:
book = {}
book_name = tr.xpath('td/a/text()')[0]
book_url = tr.xpath('td/a/@href')[0]
author = tr.xpath('td/text()')[0]
book['name'] = book_name
book['url'] = base_url + book_url # 拼接完整url
book['author'] = author
book_list.append(book)
print(book_list)
# 最近更新
trs = html.xpath('//table/tbody')[1].xpath('tr')
for tr in trs:
book = {}
book_name = tr.xpath('td/a/text()')[0]
book_url = tr.xpath('td/a/@href')[0]
author = tr.xpath('td/text()')[1]
book['name'] = book_name
book['url'] = base_url + book_url # 拼接完整url
book['author'] = author
book_list.append(book)
print(book_list)
# 小说完整目录的url
res = requests.get('http://www.biqugei.net/page/detail165482.html')
html = etree.HTML(res.text)
catagory_url = html.xpath('/html/body/div[1]/div[1]/div/div/div[2]/div/a[2]/@href')
print(catagory_url)
# 小说完整目录的所有url
# 第一种根据page拼接url 第二种,直接获取url,这里使用第二种
res = requests.get('https://www.biqugei.net/catalog/165547.html')
html = etree.HTML(res.text)
page_urls = html.xpath('//*[@id="indexselect"]')[0].xpath('option')
for page_url in page_urls:
url = page_url.xpath('@value')[0]
url = 'https://www.biqugei.net' + url
print(url)
#
# 小说目录及对应的url
catologs = html.xpath('//dl/dd')
for catolog in catologs:
catolog_name = catolog.xpath('a/text()')[0]
catolog_url = 'https://www.biqugei.net/' + catolog.xpath('a/@href')[0]
print(catolog_name, catolog_url)
# 小说章节标题以及小说章节内容
res = requests.get('https://www.biqugei.net/read/165547/406054.html')
html = etree.HTML(res.text)
title = html.xpath('//h1/text()')[0]
content = ''.join(html.xpath('//div[@id="booktxt"]/p/text()'))
print(title)
print(content)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
内容概要:学习使用爬虫技术从网络爬取小说 技术:Python,requests,xpath,lxml 能学到什么:Python爬虫基础,requests基础,xpath数据提取。 适用人群:软件开发与爬虫学习人群,具有一定的编程技术 使用场景:爬虫学习与研究 阅读建议:此资源在于综合学习Python爬虫技术,不仅是代码编写实现也更注重内容上的需求分析和方案设计,所以在学习的过程要结合这些内容一起来实践,并调试对应的代码。 其他说明:本资源只能用于学习与研究,不可用于非法用途。 文章链接:https://blog.csdn.net/iwantrain/article/details/130551354
资源推荐
资源详情
资源评论
收起资源包目录
crawl_learn.zip (1个子文件)
crawl_learn.py 3KB
共 1 条
- 1
资源评论
侠~~
- 粉丝: 122
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 第12章spring-mvc自定义类型转换器
- 基于PHP图书管理系统实验报告.docx
- Python爬取淘宝热卖商品并可视化分析
- 5152单片机proteus仿真和源码将按键次数写入AT24C02再读出并用1602LCD显示
- SE-SSD复现过程(Det3D的安装教程)
- 基于Python的在线学习与推荐系统设计与实现(论文+源码)-kaic
- 串口通过 YMODEM 协议进行文件传输
- 蓝桥杯2024年第十五届省赛真题-前缀总分
- com.qihoo.appstore_300101305-1.apk
- tensorflow-gpu-2.7.1-cp37-cp37m-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功