1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import requests from bs4 import BeautifulSoup import json import argparse
OLD_IDS_PATH = 'old_ids.txt' LEMMA_PATH = 'lemmas.txt'
''' 先读取文件,不要发以前发过的 '''
def load_dump_obj(path): ids = None try: with open(path, 'r') as f: ids = json.load(f) except: pass return ids
def dump_file(path, obj): with open(path, 'w') as f: json.dump(obj, f)
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' }
def get_id(): url = "http://127.0.0.1:5000/openqq/get_friend_info" friends = requests.get(url, headers=headers) friends.encoding = 'utf-8' for f in json.loads(friends.text): if f['name'] == '孤独的寂寞': return f['id'] return ''
def get_content(): ''' 获取baike ''' lemmas = load_dump_obj(LEMMA_PATH) if not lemmas or len(lemmas) == 0: yu_url = "https://baike.baidu.com/wikitag/api/getlemmas" data = { 'limit': 3500, 'timeout': 3000, 'tagId': 76613, 'fromLemma': False, 'contentLength': 40, 'page': 0 } lemmas = requests.post(yu_url, headers=headers, data=data).json() print(lemmas) dump_file(LEMMA_PATH, lemmas) ids = load_dump_obj(OLD_IDS_PATH) if ids is None: ids = [] curr_lemma = None for lemma in lemmas['lemmaList']: if lemma['lemmaId'] not in ids: curr_lemma = lemma ids.append(lemma['lemmaId']) dump_file(OLD_IDS_PATH, list(ids)) break
content = "请收下我诚挚的祝福" if not curr_lemma: return content url = curr_lemma['lemmaUrl'] page = requests.get(url, headers=headers) page.encoding = 'utf-8' bs = BeautifulSoup(page.text, "html.parser") summary = bs.find(name="div", attrs={"class": "lemma-summary"}) return curr_lemma['lemmaTitle'] + '\n' + summary.get_text()
def send_msg(id, content): url = 'http://127.0.0.1:5000/openqq/send_friend_message' data = { 'id': id, 'content': content } response = requests.post(url, data=data, headers=headers)
def arg_parse(): parser = argparse.ArgumentParser(description="发送QQ信息") parser.add_argument('id', help="qq friend id") return parser.parse_args()
if __name__ == '__main__': id = get_id() content = get_content() send_msg(id, content)
|