Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张益达_py
爬虫100例(复盘中)
提交
adddc2e5
爬
爬虫100例(复盘中)
项目概览
张益达_py
/
爬虫100例(复盘中)
与 Fork 源项目一致
Fork自
梦想橡皮擦 / 爬虫100例(复盘中)
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
爬
爬虫100例(复盘中)
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
adddc2e5
编写于
7月 26, 2021
作者:
梦想橡皮擦
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
书伴网数据爬虫
上级
d7345539
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
56 addition
and
0 deletion
+56
-0
案例11/书伴网数据采集.py
案例11/书伴网数据采集.py
+56
-0
未找到文件。
案例11/书伴网数据采集.py
0 → 100644
浏览文件 @
adddc2e5
import
requests
from
lxml
import
etree
# 导入协程模块
import
asyncio
import
aiohttp
headers
=
{
"User-Agent"
:
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
,
"Host"
:
"www.shuban.net"
,
"Accept"
:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}
async
def
get_content
(
url
):
print
(
"正在操作:{}"
.
format
(
url
))
# 创建一个session 去获取数据
async
with
aiohttp
.
ClientSession
()
as
session
:
async
with
session
.
get
(
url
,
headers
=
headers
)
as
res
:
if
res
.
status
==
200
:
source
=
await
res
.
text
()
# 等待获取文本
tree
=
etree
.
HTML
(
source
)
await
async_content
(
tree
)
async
def
async_content
(
tree
):
title
=
tree
.
xpath
(
"//h1[@class='title']/a/text()"
)[
0
]
print
(
title
)
# 如果页面没有信息,直接返回即可
# if title == '':
# return
# else:
# try:
# description = tree.xpath("//div[@class='hanghang-shu-content-font']")
# author = description[0].xpath("p[1]/text()")[0].replace("作者:","") if description[0].xpath("p[1]/text()")[0] is not None else None
# cate = description[0].xpath("p[2]/text()")[0].replace("分类:","") if description[0].xpath("p[2]/text()")[0] is not None else None
# douban = description[0].xpath("p[3]/text()")[0].replace("豆瓣评分:","") if description[0].xpath("p[3]/text()")[0] is not None else None
# # 这部分内容不明确,不做记录
# #des = description[0].xpath("p[5]/text()")[0] if description[0].xpath("p[5]/text()")[0] is not None else None
# download = tree.xpath("//a[@class='downloads']")
# except Exception as e:
# print(title)
# return
# ls = [
# title,author,cate,douban,download[0].get('href')
# ]
# return ls
if
__name__
==
'__main__'
:
url_format
=
"https://www.shuban.net/read-{}.html"
full_urllist
=
[
url_format
.
format
(
i
)
for
i
in
range
(
50773
,
50783
)]
# 控制到第3页,更多数据自行获取
loop
=
asyncio
.
get_event_loop
()
tasks
=
[
asyncio
.
ensure_future
(
get_content
(
url
))
for
url
in
full_urllist
]
results
=
loop
.
run_until_complete
(
asyncio
.
wait
(
tasks
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录