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
7c2c462d
Commit
7c2c462d
authored
Dec 01, 2020
by
haowang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix code
parent
ea702309
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
14 deletions
+10
-14
content_refresh.py
tasks/zhihu/content_refresh.py
+10
-14
No files found.
tasks/zhihu/content_refresh.py
View file @
7c2c462d
...
@@ -23,6 +23,8 @@ class RefreshContent(object):
...
@@ -23,6 +23,8 @@ class RefreshContent(object):
'''
'''
初始化数据库,调整js规则
初始化数据库,调整js规则
'''
'''
self
.
update_error_content_id
=
[]
self
.
conn
=
pymysql
.
connect
(
host
=
HOST
,
port
=
PORT
,
user
=
USER
,
self
.
conn
=
pymysql
.
connect
(
host
=
HOST
,
port
=
PORT
,
user
=
USER
,
passwd
=
PASSWD
,
passwd
=
PASSWD
,
db
=
DB
,
charset
=
'utf8'
)
db
=
DB
,
charset
=
'utf8'
)
...
@@ -48,26 +50,20 @@ class RefreshContent(object):
...
@@ -48,26 +50,20 @@ class RefreshContent(object):
item
.
replace_with
(
new_rich_obj
)
item
.
replace_with
(
new_rich_obj
)
return
rich_obj
.
decode
()
return
rich_obj
.
decode
()
def
create_new_content
(
self
,
content
,
pic_dict
):
def
create_new_content
(
self
,
content
_id
,
content
,
pic_dict
):
content
=
self
.
replace_html_image_to_url
(
content
)
content
=
self
.
replace_html_image_to_url
(
content
)
rich_obj
=
BeautifulSoup
(
content
,
features
=
"html.parser"
)
rich_obj
=
BeautifulSoup
(
content
,
features
=
"html.parser"
)
for
item
in
rich_obj
.
find_all
(
"img"
):
for
item
in
rich_obj
.
find_all
(
"img"
):
url
=
item
.
get
(
"src"
)
url
=
item
.
get
(
"src"
)
new_url
=
pic_dict
.
get
(
url
)
new_url
=
pic_dict
.
get
(
url
)
if
not
new_url
:
self
.
update_error_content_id
.
append
({
content_id
:
url
})
print
({
content_id
:
url
})
continue
item
[
'src'
]
=
new_url
+
'-w'
item
[
'src'
]
=
new_url
+
'-w'
return
rich_obj
.
decode
()
return
rich_obj
.
decode
()
@staticmethod
def
replace_url_to_new_url
(
content
,
url_dict
):
rich_obj
=
BeautifulSoup
(
content
,
features
=
"html.parser"
)
for
item
in
rich_obj
.
find_all
(
"img"
):
url
=
item
.
get
(
"src"
)
new_url
=
url_dict
.
get
(
url
)
item
[
'src'
]
=
new_url
return
rich_obj
.
decode
()
def
get_all_content_ids
(
self
,
table
,
key_id
):
def
get_all_content_ids
(
self
,
table
,
key_id
):
sql
=
"""select distinct {} from {}"""
.
format
(
key_id
,
table
)
sql
=
"""select distinct {} from {}"""
.
format
(
key_id
,
table
)
self
.
cur
.
execute
(
sql
)
self
.
cur
.
execute
(
sql
)
...
@@ -97,9 +93,7 @@ class RefreshContent(object):
...
@@ -97,9 +93,7 @@ class RefreshContent(object):
self
.
conn
.
commit
()
self
.
conn
.
commit
()
content
=
res
[
0
][
0
]
content
=
res
[
0
][
0
]
new_content
=
self
.
create_new_content
(
content
,
pic_dict
)
new_content
=
self
.
create_new_content
(
content_id
,
content
,
pic_dict
)
print
(
new_content
)
import
pdb
;
pdb
.
set_trace
()
sql
=
"""update {} set new_content = '{}' WHERE {} = '{}' """
.
format
(
table
,
new_content
,
key_id
,
content_id
)
sql
=
"""update {} set new_content = '{}' WHERE {} = '{}' """
.
format
(
table
,
new_content
,
key_id
,
content_id
)
self
.
cur
.
execute
(
sql
)
self
.
cur
.
execute
(
sql
)
...
@@ -120,4 +114,6 @@ if __name__ == '__main__':
...
@@ -120,4 +114,6 @@ if __name__ == '__main__':
refresh
.
refresh_content
(
'zhihu_article'
,
'zhihu_article_picture_url'
,
'article_id'
)
refresh
.
refresh_content
(
'zhihu_article'
,
'zhihu_article_picture_url'
,
'article_id'
)
elif
mark
==
2
:
elif
mark
==
2
:
refresh
.
refresh_content
(
'zhihu_thought'
,
'zhihu_thought_picture_url'
,
'thought_id'
)
refresh
.
refresh_content
(
'zhihu_thought'
,
'zhihu_thought_picture_url'
,
'thought_id'
)
print
(
refresh
.
update_error_content_id
)
print
(
datetime
.
now
())
print
(
datetime
.
now
())
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