Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张益达_py
爬虫100例(复盘中)
提交
56e2765e
爬
爬虫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,发现更多精彩内容 >>
提交
56e2765e
编写于
7月 26, 2021
作者:
梦想橡皮擦
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
阳光问政网爬虫
上级
a74b2598
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
43 addition
and
0 deletion
+43
-0
案例9/河北阳光问政平台实话实说.py
案例9/河北阳光问政平台实话实说.py
+43
-0
未找到文件。
案例9/河北阳光问政平台实话实说.py
0 → 100644
浏览文件 @
56e2765e
import
requests
import
random
from
lxml
import
etree
# 从lxml中导入etree
ua
=
[
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
,
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362'
,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
]
headers
=
{
'user-agent'
:
ua
[
random
.
randint
(
0
,
2
)]
}
for
i
in
range
(
1
,
10
):
response
=
requests
.
get
(
f
"http://yglz.tousu.hebnews.cn/shss-
{
i
}
.html"
,
headers
=
headers
)
html
=
response
.
content
.
decode
(
"utf-8"
)
print
(
"*"
*
200
)
tree
=
etree
.
HTML
(
html
)
# 解析html
divs
=
tree
.
xpath
(
'//div[@class="listcon"]'
)
# 解析列表区域div
for
div
in
divs
:
# 循环这个区域
try
:
# 注意下面是通过div去进行的xpath查找,同时加上try方式报错
shouli
=
div
.
xpath
(
'span[1]/p/a/text()'
)[
0
]
# 受理单位
content
=
div
.
xpath
(
'span[2]/p/a/text()'
)[
0
]
# 投诉内容
datetime
=
div
.
xpath
(
'span[3]/p/text()'
)[
0
].
replace
(
"
\n
"
,
""
)
# 时间
status
=
div
.
xpath
(
'span[5]/p/text()'
)[
0
].
replace
(
"
\n
"
,
""
)
# 时间
one_data
=
{
"shouli"
:
shouli
,
"type"
:
type
,
"content"
:
content
,
"datetime"
:
datetime
,
"status"
:
status
,
}
print
(
one_data
)
# 打印数据,方便存储到mongodb里面
except
Exception
as
e
:
print
(
"内部数据报错"
)
print
(
div
)
continue
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录