七、关于md5

新建一个utils文件存放我们常用的函数

md5干嘛用的?

把url变成固定长度的值

import hashlib

defget_md5(url):

ifisinstance(url,str):#py3中没有unicode关键是,判断如果为string,就encode成utf8形式

url = url.encode("utf-8")

m = hashlib.md5()

m.update(url)

returnm.hexdigest()

defextract_num(text):

#从字符串中提取出数字

match_re = re.match(".*?(\d+).*", text)

ifmatch_re:

nums =int(match_re.group(1))

else:

nums =0

returnnums

if__name__ =="__main__":

print(get_md5("http://jobbole.com".encode("utf-8")))

jobbole.py需要调用frommm.untils.commonimportget_md5

article_item["url_object_id"] = get_md5(response.url)

你可能感兴趣的:(七、关于md5)