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
| import requests import json import pymysql from multiprocessing.dummy import Pool as ThreadPool
def get_source(page): url = "http://www.bilibili.com/activity/likes/list/10156?pagesize=49&page=" + str(page) response = requests.get(url).text jsDict = json.loads(response) if jsDict['code'] == 0: list_1 = jsDict['data']['list'] for each in list_1: id = each['id'] print id sid = each['sid'] state = each['state'] type = each['type'] mid = each['mid'] wid = each['wid'] ctime = each['ctime'] likes = each['likes'] liked = each['liked'] message = each['message'] device = each['device'] image = each['image'] plat = each['plat'] reply = each['reply'] link = each['link']
owner_mid = each['owner']['mid'] owner_name = each['owner']['name'] owner_face = each['owner']['face'] owner_sex = each['owner']['sex']
owner_level_info_current_level = each['owner']['level_info']['current_level'] owner_level_info_current_min = each['owner']['level_info']['current_min'] owner_level_info_current_exp = each['owner']['level_info']['current_exp'] owner_level_info_next_exp = each['owner']['level_info']['next_exp']
try: connection = pymysql.connect( host='localhost', user='root', passwd='***', db='bilibili', charset='utf8') with connection.cursor() as cursor: sql = "INSERT INTO `yourname` (`id`,`sid`,`state`,`type`,`mid`,`wid`,`ctime`,`likes`,`liked`, `message`,`device`,`image`,`plat`, `reply`,`link`,`owner_mid`,`owner_name`,`owner_face`, `owner_sex`,`owner_level_info_current_level`,`owner_level_info_current_min`,`owner_level_info_current_exp`,`owner_level_info_next_exp`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" cursor.execute(sql, ( id, sid, state, type, mid, wid, ctime, likes, liked, message, device, image, plat, reply, link, owner_mid, owner_name, owner_face, owner_sex, owner_level_info_current_level, owner_level_info_current_min, owner_level_info_current_exp, owner_level_info_next_exp))
connection.commit() except Exception as e: print e finally: connection.close() else: print "Error"
i_1 = [] for i in range(0, 2030): i_1.append(i)
pool = ThreadPool(500) try: results = pool.map(get_source, i_1) except Exception as e: print e pool.close() pool.join()
pool.close() pool.join()
|