Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Miykael_xxm
爬虫100例(复盘中)
提交
a74b2598
爬
爬虫100例(复盘中)
项目概览
Miykael_xxm
/
爬虫100例(复盘中)
与 Fork 源项目一致
Fork自
梦想橡皮擦 / 爬虫100例(复盘中)
通知
2
Star
0
Fork
1
代码
文件
提交
分支
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,发现更多精彩内容 >>
提交
a74b2598
编写于
7月 22, 2021
作者:
梦想橡皮擦
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
蜂鸟网爬虫2
上级
60e25bbb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
77 addition
and
0 deletion
+77
-0
案例7/demo.py
案例7/demo.py
+77
-0
案例7/images/0c90bc2b073ff55e4de0d567ed8c6803.jpg
案例7/images/0c90bc2b073ff55e4de0d567ed8c6803.jpg
+0
-0
未找到文件。
案例7/demo.py
0 → 100644
浏览文件 @
a74b2598
import
re
import
json
import
time
import
os
import
asyncio
import
aiohttp
async
def
fetch_img_url
(
num
):
url
=
f
'http://bbs.fengniao.com/forum/forum_101_
{
num
}
_lastpost.html'
print
(
url
)
headers
=
{
'User-Agent'
:
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6726.400 QQBrowser/10.2.2265.400'
,
}
async
with
aiohttp
.
ClientSession
()
as
session
:
# 获取轮播图地址
async
with
session
.
get
(
url
,
headers
=
headers
)
as
response
:
try
:
url_format
=
"http://bbs.fengniao.com/forum/pic/slide_101_{0}_{1}.html"
html
=
await
response
.
text
()
# 获取到网页源码
pattern
=
re
.
compile
(
'<div class="picList">([\s\S.]*?)</div>'
)
first_match
=
pattern
.
findall
(
html
)
href_pattern
=
re
.
compile
(
'href="/forum/(\d+?)_p(\d+?)\.html'
)
urls
=
[
url_format
.
format
(
href_pattern
.
search
(
url
).
group
(
1
),
href_pattern
.
search
(
url
).
group
(
2
))
for
url
in
first_match
]
for
img_slider
in
urls
:
try
:
async
with
session
.
get
(
img_slider
,
headers
=
headers
)
as
slider
:
slider_html
=
await
slider
.
text
()
# 获取到网页源码
try
:
pic_list_pattern
=
re
.
compile
(
'var picList = eval\(
\'
\(
\'
\+
\'
\[(.*?)\]
\'
\+
\'
\)
\'
\);'
)
pic_list
=
"[{}]"
.
format
(
pic_list_pattern
.
search
(
slider_html
).
group
(
1
))
pic_json
=
json
.
loads
(
pic_list
)
# 图片列表已经拿到
print
(
pic_json
)
except
Exception
as
e
:
print
(
"代码调试错误"
)
print
(
"*"
*
100
)
print
(
e
)
for
img
in
pic_json
:
try
:
img
=
img
[
"downloadPic"
]
async
with
session
.
get
(
img
,
headers
=
headers
)
as
img_res
:
imgcode
=
await
img_res
.
read
()
with
open
(
"images/{}"
.
format
(
img
.
split
(
'/'
)[
-
1
]),
'wb'
)
as
f
:
f
.
write
(
imgcode
)
f
.
close
()
except
Exception
as
e
:
print
(
"图片下载错误"
)
print
(
e
)
continue
except
Exception
as
e
:
print
(
"获取图片列表错误"
)
print
(
img_slider
)
print
(
e
)
continue
print
(
"{}已经操作完毕"
.
format
(
url
))
except
Exception
as
e
:
print
(
"基本错误"
)
print
(
e
)
loop
=
asyncio
.
get_event_loop
()
tasks
=
[
fetch_img_url
(
num
)
for
num
in
range
(
1
,
2
)]
results
=
loop
.
run_until_complete
(
asyncio
.
wait
(
tasks
))
\ No newline at end of file
案例7/images/0c90bc2b073ff55e4de0d567ed8c6803.jpg
0 → 100644
浏览文件 @
a74b2598
1.1 MB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录