8.15爬虫笔记3

分析雪球网  https://xueqiu.com/#/property

第一次进去后,第一次Ajax请求得到的是 如下图所示的 max_id=-1, count=10。

然后往下拉,第二次Ajax请求,如下图 发现URL里面就max_id 和count不同,max_id为前一次Ajax的最后一条数据的id,以后的每次请求都是count=15,所以需要对url进行拼接

url拼接如下:

url='https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id={}&count={}&category=111'.format(str(max_id), str(count))

然后就是对数据进行定位操作了 通过python字典,列表,还有json等等,将需要的内容定位处理,然后连接数据库,将数据存储到数据库 代码如下:

  1. import requests

  2. import json

  3. import pymysql

  4.  
  5. # 打开数据库连接

  6. db = pymysql.connect(host="localhost", user="root", password="8888", database="test")

  7. # 使用 cursor() 方法创建一个游标对象 cursor

  8. cursor = db.cursor()

  9. # ————————————————————————————————————————————————————————————————

  10. i = 1 # 用来控制要爬取的页面数

  11. count = 10 # 因为第一页和其他页面的count不同,其他页面为15

  12. max_id = -1

  13. while i < 10:

  14. '''第一个循环用来控制要爬取数据的 总ajax请求的次数'''

  15. url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id={}&count={}&category=111'.format(str(max_id), str(count))

  16. headers = {

  17. 'User-Agent': '''Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36

  18. (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

  19. ''',

  20. 'Cookie': '''aliyungf_tc=AQAAABS6+HG0OAQAUhVFeZTrWYKcrmDe;

  21. xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4;

  22. xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA;

  23. xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a;

  24. xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs;

  25. _ga=GA1.2.2007990410.1534303926;

  26. _gid=GA1.2.1454932696.1534303926;

  27. u=781534303927452; device_id=0883ecbffed505f2f843656aec9a0524;

  28. Hm_lvt_1db88642e346389874251b5a1eded6e3=1534303929,1534303938;

  29. _gat_gtag_UA_16079156_4=1;

  30. Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534323392

  31. '''

  32. }

  33. response = requests.get(url, headers=headers)

  34.  
  35. msg = response.content.decode('utf-8')

  36. msg_dict = json.loads(msg)

  37. # print(type(msg_dict))

  38. # print(msg_dict)

  39. if count == 10:

  40. max_id = msg_dict['next_id']

  41. else:

  42. max_id = msg_dict['next_max_id']

  43.  
  44. flag = 0

  45. while flag < count:

  46. data = msg_dict['list'][flag]['data']

  47. flag += 1

  48. data_dict = json.loads(data)

  49. # print(data_dict)

  50.  
  51. uid = data_dict['id']

  52. title = data_dict['title']

  53. description = data_dict['description']

  54. target = data_dict['target']

  55. # ---------------------添加到数据库-----------------------------

  56. # SQL 插入语句

  57. sql = """INSERT INTO xueqiu(uid, title, description, target)

  58. VALUES ('{}', '{}', '{}', '{}')""".format(uid, title, description, target)

  59. try:

  60. # 执行sql语句

  61. cursor.execute(sql)

  62. # 提交到数据库执行

  63. db.commit()

  64. except:

  65. # 如果发生错误则回滚

  66. db.rollback()

  67.  
  68. count = 15

  69. i = i + 1

  70. # 关闭数据库连接

  71. db.close()

猜你喜欢

转载自blog.csdn.net/Lujuntong/article/details/81711477
今日推荐