sunsexsurf

get_hash_coins

Apr 13th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import SQLHelper
  2. import io
  3. import requests
  4. import imagehash
  5. from PIL import Image
  6. import re
  7. import csv
  8. import logging
  9.  
  10. photopath = "http://cbr.ru/legacy/PhotoStore/img/"
  11. descpath = "http://cbr.ru/cash_circulation/memorable_coins/coins_base/ShowCoins/?cat_num="
  12.  
  13.  
  14. def main():
  15.     logging.basicConfig(filename="coinsparse.log", level=logging.INFO)
  16.     db = SQLHelper.SQLHelper("test.db")
  17.     coins = db["coins"]("id", "cat_number")
  18.     csfile = open("coinhash.csv", "w", newline="")
  19.     csw = csv.writer(csfile, dialect="excel", delimiter =";")
  20.     csw.writerow(["id","cat_number","mint","phash"])
  21.     for coin in coins:
  22.         try:
  23.             hash = imagehash.phash(Image.open(io.BytesIO(requests.get(f"{photopath}{coin[1]}r.jpg").content)))
  24.             page = requests.get(descpath+coin[1]).text
  25.             mint = re.search("Чеканка:(.+?)\.",page)
  26.             if mint:
  27.                 mint = mint.group(1).strip()
  28.             csw.writerow([coin[0],coin[1],mint, hash])
  29.         except Exception:
  30.             logging.info("Error parsing cat_number :"+coin[1])
  31.     csfile.close()
  32.  
  33. if __name__ == "__main__":
  34.     main()
Advertisement
Add Comment
Please, Sign In to add comment