jackpieno

D卡修改

Jun 8th, 2021
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import requests
  4. import csv
  5. import random
  6. import json
  7. import pickle
  8. from fake_useragent import UserAgent
  9. from time import sleep
  10.  
  11.  
  12. ua = UserAgent()
  13. headers = {
  14. 'User-Agent': ua.random,
  15. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  16. 'Accept-Language': 'h-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,vi;q=0.6,zh-CN;q=0.5',
  17. 'Accept-Encoding': 'gzip, deflate, br',
  18. 'Prefer': 'safe',
  19. 'Content-Type': 'application/x-www-form-urlencoded',
  20. 'Connection': 'keep-alive',
  21. 'Cookie': 'dcsrd=0lpqI2DEQ-OCic1WrT8pUwH6;',
  22. 'Upgrade-Insecure-Requests': '1',
  23. 'Pragma': 'no-cache',
  24. 'Cache-Control': 'no-cache'
  25.                                        }
  26.    
  27.  
  28.    
  29. 折扣 = []
  30. 打折 = []
  31. 免運 = []
  32. 代言 = []
  33.  
  34. before_api = ''
  35. i = 0
  36. while True:
  37.     try:
  38.         api = 'https://www.dcard.tw/service/api/v2/forums/buyonline/posts?popular=false&limit=100&' + before_api
  39.         response = requests.get(api,headers=headers)
  40.         # response = scraper.get(api,headers=headers).text
  41.         posts = response.json()
  42.         # posts = json.loads(response)
  43.         i += len(posts)
  44.         #print('1')
  45.         for post in posts:
  46.             pid = post['id']
  47.             date = post['createdAt']
  48.  
  49.             post_url = f'https://www.dcard.tw/service/api/v2/posts/{pid}'
  50.             response = requests.get(post_url,headers=headers)
  51.             article = response.json()
  52.             #print('2')
  53.  
  54.             comments = []
  55.             comment_url = f'https://www.dcard.tw/service/api/v2/posts/{pid}/comments?popular=false&limit=100'
  56.             response2 = requests.get(comment_url,headers=headers)
  57.             all_comments = response2.json()
  58.             #print(all_comments);
  59.             for comment in all_comments:
  60.                 try:
  61.                     comment_content = comment['content'].replace(',',',')
  62.                     comments.append(comment_content)
  63.                 except:
  64.                     pass
  65.  
  66.             title = article['title'].replace(',',',')
  67.             content = article['content'].replace(',',',')
  68.             content = ' '.join(content.split())
  69.  
  70.             has_shopee = False
  71.             if '蝦皮' in content or '蝦皮' in title:
  72.                 has_shopee = True
  73.  
  74.             if '折扣' in title:
  75.                 折扣.append([title,has_shopee,content,comments])
  76.  
  77.             if '打折' in title:
  78.                 打折.append([title,has_shopee,content,comments])
  79.  
  80.             if '免運' in title:
  81.                 免運.append([title,has_shopee,content,comments])  
  82.  
  83.             if '代言' in title:
  84.                 代言.append([title,has_shopee,content,comments])
  85.        
  86.         before_api = f'before={pid}'
  87.         print(f'\r {i} , 折扣 {len(折扣)} , 免運 {len(免運)}, 代言 {len(代言)}, 打折 {len(打折)}',end='')
  88.        
  89.         if i>= 30000:
  90.             break
  91.     except Exception as e:
  92.         print(e)
  93.         break
Advertisement
Add Comment
Please, Sign In to add comment