1 Star 0 Fork 1

麦琪 / 饿了么_兴盛优选_爬虫(含经纬度城市遍历)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cache.py 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
- 提交于 2020-04-09 23:15 . 首先,要有光
#!/usr/bin/env python
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import traceback
from time import sleep
import rocksdb
from os.path import join,dirname,abspath
from requests import Session
from hashlib import blake2b
from json import loads
ROOT = dirname(abspath(__file__))
class Cache:
def __init__(self, db):
self.session = Session()
self._db = rocksdb.DB(
join(ROOT,'cache',db),
rocksdb.Options(create_if_missing=True)
)
def get(self, url, headers={}, verify=lambda x:True):
cache = self._db
key = blake2b(url.encode('utf-8','ignore')).digest()
r = cache.get(key)
if r:
# print('cached')
r = loads(r)
return r
# headers.update(proxy_header())
while 1:
try:
r = self.session.get(
url,
timeout=60,
verify=False,
# proxies=dict(http=PROXY, https=PROXY),
headers=headers
)
j = r.json()
if verify(j):
cache.put(
key,
r.content
)
return j
except Exception:
traceback.print_exc()
1
https://gitee.com/emaiqi/elem.git
git@gitee.com:emaiqi/elem.git
emaiqi
elem
饿了么_兴盛优选_爬虫(含经纬度城市遍历)
master

搜索帮助