Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
crawler
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chengyang Zhong
crawler
Commits
f1a51df3
Commit
f1a51df3
authored
Nov 26, 2020
by
haowang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify
parent
778c9506
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
20 deletions
+41
-20
crawler_zhihu_test.py
crawler_sys/site_crawler/crawler_zhihu_test.py
+41
-20
No files found.
crawler_sys/site_crawler/crawler_zhihu_test.py
View file @
f1a51df3
...
...
@@ -22,15 +22,38 @@ import cv2
from
pymysql
import
escape_string
HOST
=
'172.18.51.14'
PORT
=
'3306'
USER
=
'spider'
PASSWD
=
'Gengmei123'
DB
=
'spider'
JS_FILE_PATH
=
'/srv/apps/crawler/crawler_sys/site_crawler/zhihu.js'
SPIDER_URL
=
'https://www.zhihu.com/people/geng-mei-suo-chang/answers?page=1'
ANSWER_URL
=
'https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/answers?include=data
%5
B*
%5
D.is_normal
%2
Cadmin_closed_comment
%2
Creward_info
%2
Cis_collapsed
%2
Cannotation_action
%2
Cannotation_detail
%2
Ccollapse_reason
%2
Ccollapsed_by
%2
Csuggest_edit
%2
Ccomment_count
%2
Ccan_comment
%2
Ccontent
%2
Ceditable_content
%2
Cvoteup_count
%2
Creshipment_settings
%2
Ccomment_permission
%2
Cmark_infos
%2
Ccreated_time
%2
Cupdated_time
%2
Creview_info
%2
Cexcerpt
%2
Cis_labeled
%2
Clabel_info
%2
Crelationship.is_authorized
%2
Cvoting
%2
Cis_author
%2
Cis_thanked
%2
Cis_nothelp
%2
Cis_recognized
%3
Bdata
%5
B*
%5
D.author.badge
%5
B
%3
F(type
%3
Dbest_answerer)
%5
D.topics
%3
Bdata
%5
B*
%5
D.question.has_publishing_draft
%2
Crelationship&offset={0}&limit=20&sort_by=created'
ARTICLE_URL
=
'https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/articles?include=data
%5
B*
%5
D.comment_count
%2
Csuggest_edit
%2
Cis_normal
%2
Cthumbnail_extra_info
%2
Cthumbnail
%2
Ccan_comment
%2
Ccomment_permission
%2
Cadmin_closed_comment
%2
Ccontent
%2
Cvoteup_count
%2
Ccreated
%2
Cupdated
%2
Cupvoted_followees
%2
Cvoting
%2
Creview_info
%2
Cis_labeled
%2
Clabel_info
%3
Bdata
%5
B*
%5
D.author.badge
%5
B
%3
F(type
%3
Dbest_answerer)
%5
D.topics&offset={0}&limit=20&sort_by=created'
THOUGHT_URL
=
'https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/pins?offset={0}&limit=20&includes=data
%5
B*
%5
D.upvoted_followees
%2
Cadmin_closed_comment'
ANSWER_ROOT_COMMENT_URL
=
'https://www.zhihu.com/api/v4/answers/{0}/root_comments?order=normal&limit=20&offset={1}&status=open'
ARTICLE_ROOT_COMMENT_URL
=
'https://www.zhihu.com/api/v4/articles/{0}/root_comments?order=normal&limit=20&offset={1}&status=open'
CHILD_COMMENT_START_URL
=
'https://www.zhihu.com/api/v4/comments/{0}/child_comments'
CHILD_COMMENT_OFFSET_URL
=
'https://www.zhihu.com/api/v4/comments/{0}/child_comments?limit=20&offset={1}'
THOUGHT_COMMENT_URL
=
'https://www.zhihu.com/api/v4/pins/{0}/comments?order=normal&limit=20&offset={1}&status=open'
ANSWER_PICTURE_PATH
=
'/data/answer_picture/'
ARTICLE_PICTURE_PATH
=
'/data/article_picture/'
THOUGHT_PICTURE_PATH
=
'/data/thought_picture/'
ANSWER_PICTURE_CUT_PATH
=
'/data/answer_picture_cut/'
ARTICLE_PICTURE_CUT_PATH
=
'/data/article_picture_cut/'
THOUGHT_PICTURE_CUT_PATH
=
'/data/thought_picture_cut/'
class
Crawler_zhihu
():
def
__init__
(
self
):
'''
初始化数据库,调整js规则
'''
self
.
conn
=
pymysql
.
connect
(
host
=
'bj-cdb-b8oeejac.sql.tencentcdb.com'
,
port
=
62118
,
user
=
'work'
,
passwd
=
'Gengmei1'
,
db
=
'mimas_dev'
,
charset
=
'utf8'
)
self
.
conn
=
pymysql
.
connect
(
host
=
HOST
,
port
=
PORT
,
user
=
USER
,
passwd
=
PASSWD
,
db
=
DB
,
charset
=
'utf8'
)
self
.
cur
=
self
.
conn
.
cursor
()
# self.cur.execute("drop table if exists zhihu_answer")
# sql = """create table zhihu_answer(title char(40),
...
...
@@ -120,7 +143,7 @@ class Crawler_zhihu():
with
open
(
'./zhihu.js'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
js
=
f
.
read
()
except
:
with
open
(
'/Users/xuwei/crawler/crawler_sys/site_crawler/zhihu.js'
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
with
open
(
JS_FILE_PATH
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
js
=
f
.
read
()
# print(js)
self
.
exec_js
=
execjs
.
compile
(
js
,
cwd
=
r'/usr/local/lib/node_modules'
)
...
...
@@ -216,9 +239,9 @@ class Crawler_zhihu():
'''
offset
=
str
(
offset
)
if
mark
==
0
:
url
=
"https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/answers?include=data
%5
B*
%5
D.is_normal
%2
Cadmin_closed_comment
%2
Creward_info
%2
Cis_collapsed
%2
Cannotation_action
%2
Cannotation_detail
%2
Ccollapse_reason
%2
Ccollapsed_by
%2
Csuggest_edit
%2
Ccomment_count
%2
Ccan_comment
%2
Ccontent
%2
Ceditable_content
%2
Cvoteup_count
%2
Creshipment_settings
%2
Ccomment_permission
%2
Cmark_infos
%2
Ccreated_time
%2
Cupdated_time
%2
Creview_info
%2
Cexcerpt
%2
Cis_labeled
%2
Clabel_info
%2
Crelationship.is_authorized
%2
Cvoting
%2
Cis_author
%2
Cis_thanked
%2
Cis_nothelp
%2
Cis_recognized
%3
Bdata
%5
B*
%5
D.author.badge
%5
B
%3
F(type
%3
Dbest_answerer)
%5
D.topics
%3
Bdata
%5
B*
%5
D.question.has_publishing_draft
%2
Crelationship&offset={0}&limit=20&sort_by=created"
.
format
(
offset
)
url
=
ANSWER_URL
.
format
(
offset
)
elif
mark
==
1
:
url
=
"https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/articles?include=data
%5
B*
%5
D.comment_count
%2
Csuggest_edit
%2
Cis_normal
%2
Cthumbnail_extra_info
%2
Cthumbnail
%2
Ccan_comment
%2
Ccomment_permission
%2
Cadmin_closed_comment
%2
Ccontent
%2
Cvoteup_count
%2
Ccreated
%2
Cupdated
%2
Cupvoted_followees
%2
Cvoting
%2
Creview_info
%2
Cis_labeled
%2
Clabel_info
%3
Bdata
%5
B*
%5
D.author.badge
%5
B
%3
F(type
%3
Dbest_answerer)
%5
D.topics&offset={0}&limit=20&sort_by=created"
.
format
(
offset
)
url
=
ARTICLE_URL
.
format
(
offset
)
[
headers_search
,
cookies_dict
]
=
self
.
headers_handle
(
url
)
get_page
=
retry_get_url
(
url
,
headers
=
headers_search
,
cookies
=
cookies_dict
,
proxies
=
proxies_num
)
...
...
@@ -241,8 +264,6 @@ class Crawler_zhihu():
else
:
print
(
"article_data_error"
)
return
def
search_root_comment
(
self
,
answerid
,
offset
,
mark
,
proxies_num
=
0
):
...
...
@@ -252,9 +273,9 @@ class Crawler_zhihu():
offset
=
str
(
offset
)
answerid
=
str
(
answerid
)
if
mark
==
0
:
url
=
"https://www.zhihu.com/api/v4/answers/{0}/root_comments?order=normal&limit=20&offset={1}&status=open"
.
format
(
answerid
,
offset
)
url
=
ANSWER_ROOT_COMMENT_URL
.
format
(
answerid
,
offset
)
elif
mark
==
1
:
url
=
"https://www.zhihu.com/api/v4/articles/{0}/root_comments?order=normal&limit=20&offset={1}&status=open"
.
format
(
answerid
,
offset
)
url
=
ARTICLE_ROOT_COMMENT_URL
.
format
(
answerid
,
offset
)
[
headers_search
,
cookies_dict
]
=
self
.
headers_handle
(
url
)
...
...
@@ -310,9 +331,9 @@ class Crawler_zhihu():
offsets
=
offset
offset
=
str
(
offset
)
if
offsets
==
0
:
url
=
"https://www.zhihu.com/api/v4/comments/{0}/child_comments"
.
format
(
root_comment_id
)
url
=
CHILD_COMMENT_START_URL
.
format
(
root_comment_id
)
else
:
url
=
"https://www.zhihu.com/api/v4/comments/{0}/child_comments?limit=20&offset={1}"
.
format
(
root_comment_id
,
offset
)
url
=
CHILD_COMMENT_OFFSET_URL
.
format
(
root_comment_id
,
offset
)
[
headers_search
,
cookies_dict
]
=
self
.
headers_handle
(
url
)
get_page
=
retry_get_url_no_proxies
(
url
,
headers
=
headers_search
,
cookies
=
cookies_dict
,
proxies
=
proxies_num
)
...
...
@@ -391,7 +412,7 @@ class Crawler_zhihu():
self
.
cur
.
execute
(
sql
)
tuple
=
self
.
cur
.
fetchall
()
self
.
conn
.
commit
()
path
=
"/Users/xuwei/Desktop/answer_picture/"
path
=
ANSWER_PICTURE_PATH
gif_patt
=
r'gif'
for
i
in
range
(
len
(
tuple
)):
mark
=
re
.
search
(
gif_patt
,
tuple
[
i
][
1
])
...
...
@@ -419,7 +440,7 @@ class Crawler_zhihu():
img
=
cv2
.
imread
(
pathes
)
high
,
width
=
img
.
shape
[:
2
]
cropped
=
img
[
0
:
int
(
high
/
10
*
9
),
0
:
width
]
pathes
=
"/Users/xuwei/Desktop/answer_picture_cut/
num"
+
str
(
i
)
+
".jpg"
pathes
=
ANSWER_PICTURE_CUT_PATH
+
"
num"
+
str
(
i
)
+
".jpg"
cv2
.
imwrite
(
pathes
,
cropped
)
new_url
=
self
.
upload_image_with_path
(
pathes
)
...
...
@@ -502,7 +523,7 @@ class Crawler_zhihu():
self
.
cur
.
execute
(
sql
)
tuple
=
self
.
cur
.
fetchall
()
self
.
conn
.
commit
()
path
=
"/Users/xuwei/Desktop/article_picture/"
path
=
ARTICLE_PICTURE_PATH
gif_patt
=
r'gif'
for
i
in
range
(
len
(
tuple
)):
mark
=
re
.
search
(
gif_patt
,
tuple
[
i
][
1
])
...
...
@@ -530,7 +551,7 @@ class Crawler_zhihu():
img
=
cv2
.
imread
(
pathes
)
high
,
width
=
img
.
shape
[:
2
]
cropped
=
img
[
0
:
int
(
high
/
10
*
9
),
0
:
width
]
pathes
=
"/Users/xuwei/Desktop/article_picture_cut/
num"
+
str
(
i
)
+
".jpg"
pathes
=
ARTICLE_PICTURE_CUT_PATH
+
"
num"
+
str
(
i
)
+
".jpg"
cv2
.
imwrite
(
pathes
,
cropped
)
new_url
=
self
.
upload_image_with_path
(
pathes
)
...
...
@@ -590,7 +611,7 @@ class Crawler_zhihu():
想法数据包请求
'''
offset
=
str
(
offset
)
url
=
"https://www.zhihu.com/api/v4/members/geng-mei-suo-chang/pins?offset={0}&limit=20&includes=data
%5
B*
%5
D.upvoted_followees
%2
Cadmin_closed_comment"
.
format
(
offset
)
url
=
THOUGHT_URL
.
format
(
offset
)
[
headers_search
,
cookies_dict
]
=
self
.
headers_handle
(
url
)
get_page
=
retry_get_url
(
url
,
headers
=
headers_search
,
cookies
=
cookies_dict
,
proxies
=
proxies_num
)
...
...
@@ -644,7 +665,7 @@ class Crawler_zhihu():
'''
offset
=
str
(
offset
)
answerid
=
str
(
answerid
)
url
=
"https://www.zhihu.com/api/v4/pins/{0}/comments?order=normal&limit=20&offset={1}&status=open"
.
format
(
answerid
,
offset
)
url
=
THOUGHT_COMMENT_URL
.
format
(
answerid
,
offset
)
[
headers_search
,
cookies_dict
]
=
self
.
headers_handle
(
url
)
...
...
@@ -690,7 +711,7 @@ class Crawler_zhihu():
self
.
cur
.
execute
(
sql
)
tuple
=
self
.
cur
.
fetchall
()
self
.
conn
.
commit
()
path
=
"/Users/xuwei/Desktop/thought_picture/"
path
=
THOUGHT_PICTURE_PATH
gif_patt
=
r'gif'
for
i
in
range
(
len
(
tuple
)):
mark
=
re
.
search
(
gif_patt
,
tuple
[
i
][
1
])
...
...
@@ -718,7 +739,7 @@ class Crawler_zhihu():
img
=
cv2
.
imread
(
pathes
)
high
,
width
=
img
.
shape
[:
2
]
cropped
=
img
[
0
:
int
(
high
/
10
*
9
),
0
:
width
]
pathes
=
"/Users/xuwei/Desktop/thought_picture_cut/
num"
+
str
(
i
)
+
".jpg"
pathes
=
THOUGHT_PICTURE_CUT_PATH
+
"
num"
+
str
(
i
)
+
".jpg"
cv2
.
imwrite
(
pathes
,
cropped
)
new_url
=
self
.
upload_image_with_path
(
pathes
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment