stare112.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. '''Created May 25, 2022 deeejas'''
  2. # coding: UTF-8
  3. import re
  4. from time import sleep
  5. import requests
  6. import gspread
  7. from gspread.client import BackoffClient
  8. from bs4 import BeautifulSoup
  9. # g_sheet = '1JW9MyYjZjyLMKtj4bwanq5VbUWaIF0RhZjbQQFd2YFQ' # MZK
  10. g_sheet = '1jBc-DottcmtttoDFRMdsVVp6nbEmq4ficd58XFy4x-o'
  11. # gc = gspread.service_account(filename='Pysheet26134-2daf66659e50.json', client_factory=BackoffClient) # MZK
  12. gc = gspread.service_account(filename='Pygsheet-4a83fcaf2282.json', client_factory=BackoffClient)
  13. gsh = gc.open_by_key(g_sheet)
  14. sh1 = gsh.worksheet('PJ')
  15. col_CUI = sh1.col_values(2)[3:]
  16. col_MESSAGE = sh1.col_values(10)[3:]
  17. col_INDEX = sh1.col_values(4)[3:]
  18. cond_range = zip(col_CUI, col_MESSAGE, col_INDEX)
  19. def state_decl(decl_id: str, vat: str):
  20. payload = {
  21. 'id': decl_id,
  22. 'cui': vat}
  23. headers = {
  24. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
  25. 'Accept-Encoding': 'gzip, deflate, br',
  26. 'Accept': '*/*',
  27. 'Connection': 'keep-alive'}
  28. url = 'https://www.anaf.ro/StareD112/vizualizareStare.do'
  29. resp = requests.post(url, data=payload, headers=headers)
  30. print(resp.status_code)
  31. html = resp.content
  32. soup = BeautifulSoup(html, 'html.parser')
  33. message = []
  34. for tag in soup.find_all('tr', align='left'):
  35. # print(tag.find_all('td'))
  36. for child in tag.find_all('td'):
  37. child = child.get_text().strip()
  38. message.append(child)
  39. start = message.index(decl_id)
  40. message = message[start:start + 6]
  41. search_str = soup.find_all('td', valign='top')[0].get_text()
  42. cui = re.search('(?<=cui: )[0-9]*', search_str).group()
  43. message.insert(0, cui)
  44. print(message)
  45. # sleep(1)
  46. return message
  47. if __name__ == '__main__':
  48. for cui, message, index_ in cond_range:
  49. # print(message)
  50. condition = cui != '' and message == 'F'
  51. print(condition)
  52. if condition:
  53. state = state_decl(decl_id=index_, vat=cui)
  54. sleep(1)
  55. cell = sh1.find(index_, in_column=4)
  56. sh1.update_cell(cell.row, col=10, value=state[3])
  57. sh1.update_cell(cell.row, col=5, value=state[2])
  58. sh1.update_cell(cell.row, col=7, value=state[6])