This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql.cursors | |
# Connect to the database | |
connection = pymysql.connect(host='localhost', | |
user='user', | |
password='passwd', | |
db='db', | |
charset='utf8mb4', | |
cursorclass=pymysql.cursors.DictCursor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from w3lib.html import remove_tags, strip_html5_whitespace | |
# keep参数为需要保留的标签名称 | |
remove_tags(text, keep=('img',)) | |
# 移除HTML标签,并删除前后的空白字符 | |
def clean_tags(text, which_ones=(), keep=(), encoding=None) -> str: | |
if not text: | |
return None | |
content = remove_tags(text, which_ones, keep, encoding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.parse as urlparse | |
def get_url_param(url: str, param: str) -> str: | |
url_parse = urlparse.urlparse(url) | |
url_dict = dict(urlparse.parse_qsl(url_parse.query)) | |
return url_dict.get(param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def chunked(iterable, n): | |
from itertools import islice | |
from functools import partial | |
def take(n, iterable): | |
return list(islice(iterable, n)) | |
return iter(partial(take, n, iter(iterable)), []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
# 字典列表 | |
result_list = [] | |
with open('weibo_data.csv', 'w', newline='', encoding='gbk') as csv_file: | |
# header field | |
fieldnames = result_list[0].keys() | |
writer = csv.DictWriter(csv_file, fieldnames=fieldnames) | |
writer.writeheader() | |
writer.writerows(result_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
def base62_encode(num, alphabet=ALPHABET): | |
"""Encode a number in Base X | |
`num`: The number to encode | |
`alphabet`: The alphabet to use for encoding | |
""" | |
if (num == 0): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
import math | |
PRIMES = [ | |
112272535095293, | |
112582705942171, | |
112272535095293, | |
115280095190773, | |
115797848077099, | |
1099726899285419] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
import urllib.request | |
URLS = ['http://www.163.com/', | |
'http://www.qq.com/', | |
'http://www.baidu.com/', | |
'http://www.v2ex.com/', | |
'http://www.360.com'] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5 import QtCore, QtGui, QtWidgets | |
from PyQt5.QtWidgets import * | |
# 打开文件对话框, | |
filename, filetype = QFileDialog.getOpenFileName(None, '选择文件', "", filter="txt (*.txt)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
多进程示例 | |
""" | |
import time | |
from multiprocessing import Pool | |
def run(fn): | |
time.sleep(1) | |
print(fn) |