Guest User

parser.py

a guest
Mar 23rd, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. from requests import get, post
  2. from json import loads
  3. from time import sleep
  4.  
  5. def parse(thread, current_post, end):
  6.  
  7.     while 1:
  8.         try:
  9.             res = get(thread.replace(".html", ".json"))
  10.             if res.status_code == 200:
  11.                 for post in loads(res.text)["threads"][0]["posts"][current_post:]:
  12.                     current_post = current_post + 1
  13.                     if current_post == end:
  14.                         print("Stop parser yopta")
  15.                         return 0
  16.                     if post["files"]:
  17.                         for file in post["files"]:
  18.                             if file["type"] == 6 or file["type"] == 10 or file["type"] == 2 or file["type"] == 4 or file["type"] == 1:
  19.                                 tmp = get("https://2ch.hk" + file["path"]).content
  20.                                 tmp_file = open(file["name"], "wb")
  21.                                 tmp_file.write(tmp)
  22.                                 tmp_file.close()
  23.                 sleep(10)
  24.  
  25.             elif res.status_code == 404:
  26.                 print("%s 404 not found." % thread)
  27.                 return 0
  28.         except Exception as e:
  29.             print("Fatal error: %s" % str(e))
  30.  
  31. parse("https://2ch.hk/b/res/265324742.html", 0, 500)
Add Comment
Please, Sign In to add comment