Skip to content

Instantly share code, notes, and snippets.

View JamCh01's full-sized avatar
💭
I may be slow to respond.

JamCh01 JamCh01

💭
I may be slow to respond.
View GitHub Profile
import re
import requests
url = 'http://www.quanshuwang.com/book_113389.html'
r = requests.get(url=url)
r.encoding = 'gbk'
regex = re.compile(r'<a href="(.*?)" class="l mr11">')
novel_url = regex.findall(r.text)
print(novel_url)
@JamCh01
JamCh01 / models.py
Created December 21, 2018 08:03
models.py
from django.db import models
from django.db.models.base import ModelBase
def models_generator(db_table):
class CustomMetaClass(ModelBase):
def __new__(cls, name, bases, attrs):
model = super(CustomMetaClass, cls).__new__(
cls, name, bases, attrs)
model._meta.db_table = db_table
print(db_table)
class Base(object):
def __init__(self):
pass
def login(self, **kwargs):
# do something
pass
def logout(self, **kwargs):
# do something
@JamCh01
JamCh01 / spider.py
Last active March 26, 2018 13:43
spider.py
import requests
from bs4 import BeautifulSoup
from threading import Thread
def test():
r = requests.get('http://zhuanlan.sina.com.cn/')
print(r.encoding)
soup = BeautifulSoup(r.content.decode('utf-8', 'ignore'), 'lxml')
from itertools import combinations
_sum = 5 # 这里的 _sum 对应了『给定数』
_list = [1, 2, 3, 4, 5] # 这里就是输入的数据 对应了『一个数组』
for i in range(len(_list)):
for num in combinations(_list, i + 1): # 这里循环是题设没有讲明几个数字相加,所以我用了从一个数字到所有数字相加
if sum(num) == _sum:
print num # 这里就是结果了
@JamCh01
JamCh01 / test.py
Created January 2, 2018 18:51
jinja2_md5_filename_with_custom_filter
from jinja2 import Environment
from hashlib import md5
def ud_md5(file_name):
with open(file_name, 'rb') as f:
md_5 = md5()
md_5.update(f.read())
return md_5.hexdigest()
def test():
@JamCh01
JamCh01 / letter_decode.py
Created October 13, 2017 06:59
字母a-1,b-2……z-26,那么给一串数字 可以对应多少种字母组合。
def letter_sum(s):
return 0 if len(s) == 0 else get_number(s)
def test_number(s):
return 0 if int(s[0]) == 0 else 1 if int(s) in range(1, 27) else 0
def get_number(s):
if len(s) == 1: