drssionPage demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.3 KiB

5 months ago
  1. from DrissionPage import Chromium, ChromiumOptions
  2. class DPModel():
  3. def __init__(self, link):
  4. self.login_qr = ""
  5. self.login_link = link
  6. self.browser = self.init_browser()
  7. self.tab = None
  8. def init_browser(self):
  9. co = ChromiumOptions()
  10. co.set_argument("--remote-debugging-port", "9222")
  11. browser = Chromium(co) # 创建浏览器对象
  12. browser.set.retry_times(10) # 设置整体运行参数
  13. return browser
  14. def get_login_qr(self):
  15. try:
  16. self.tab = self.browser.latest_tab # 获取Tab对象
  17. self.tab.get("https://etax.chinatax.gov.cn/webstatic/login")
  18. qr_code = self.tab.ele('xpath://*[@id="app"]/div[2]/div/div/div[2]/div[3]/div[1]/img').attr("src")
  19. self.login_qr = qr_code
  20. except Exception as e:
  21. print(f"e is => {e}")
  22. self.browser.quit() # 关闭浏览器
  23. return self.login_qr
  24. def get_content(self):
  25. try:
  26. self.tab.wait.eles_loaded('xpath://*[@id="app"]/div[1]/div/div[2]/ul/li[3]/a', timeout=30)
  27. ele = self.tab.ele('xpath://*[@id="app"]/div[1]/div/div[2]/ul/li[3]/a') # 我要查询
  28. ele.click()
  29. self.tab.ele('xpath://*[@id="app"]/div[2]/div/div/div[1]/div[2]/a[1]').click() # 申报查询
  30. text = self.tab.ele('xpath://*[@id="app"]/div[2]/div/div[2]/div[2]/div[1]/div[1]').text # 待缴税款
  31. print(text)
  32. text1 = self.tab.ele('xpath://*[@id="app"]/div[2]/div/div[2]/div[2]/div[1]/div[2]').text # 可申请退税金额
  33. print(text1)
  34. ele = self.tab.ele('xpath://*[@id="app"]/div[2]/div/div[2]/div[1]/label[2]/span') # 已完成标签
  35. ele.click()
  36. table_list = self.tab.eles('xpath://*[@id="app"]/div[2]/div/div[2]/div[3]/div/div[3]/table/tbody/tr')
  37. for tr in table_list:
  38. # td = tab.eles('@tag:td')
  39. print(tr.texts()) # 获取所有的文本
  40. # //*[@id="app"]/div[2]/div/div[2]/div[3]/div/div[3]/table/tbody/tr[2]/td[6]/div/a[1]
  41. href = tr.ele('@tag()=a') # 跳转链接
  42. href.click()
  43. self.tab.wait.load_start() # 等待
  44. self.inner_page(self.tab)
  45. break
  46. except Exception as e:
  47. print(f"e is => {e}")
  48. self.browser.quit() # 关闭浏览器
  49. def inner_page(self, page):
  50. """
  51. """
  52. a_tab = page.ele('xpath:/html/body/div[4]/div/div/div[3]/ul/li[3]/a') # 缴税记录
  53. a_tab.click()
  54. table_list = page.eles(
  55. 'xpath:/html/body/div[4]/div/div/div[3]/div[3]/div/div/div/div[2]/div/table/tbody/tr') # 凭证
  56. for tr in table_list:
  57. href = tr.ele('@tag()=a') # 跳转链接 证明pdf 需要下载
  58. href.click() # 跳转下载
  59. a_tab.wait.doc_loaded() # 等待文档加载完毕
  60. page.get_screenshot(path='tmp', name='pic.jpg', full_page=False) # 下载发票
  61. break
  62. def __del__(self):
  63. print("?????")
  64. # self.browser.quit() # 关闭浏览器
  65. if __name__ == '__main__':
  66. url = "https://etax.chinatax.gov.cn/webstatic/login"
  67. d = DPModel(url)
  68. qr_code = d.get_login_qr() # 获取验证码
  69. print(qr_code)
  70. pass