1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| import json import random
import requests from bs4 import BeautifulSoup
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36'
def login(): session = requests.session() url = "https://einvoice.nat.gov.tw/home/randNum?id=" + str(random.random()) headers = { 'Host': "www.einvoice.nat.gov.tw", 'Referer': "https://einvoice.nat.gov.tw/index", } res = session.get(url, stream=True, headers=headers) print('產生驗證碼', url, res.status_code) if res.status_code != requests.codes.ok: return
with open('rand.jpg', 'wb') as file: for chunk in res: file.write(chunk) phone = input('輸入手機號碼:') pw = input('輸入密碼:') captcha = input('輸入驗證碼:') url = 'https://www.einvoice.nat.gov.tw/home/Ajax' payload = { 'checkString': captcha } headers = { 'User-Agent': user_agent } res = session.post(url, data=payload, headers=headers) print('檢查驗證碼', url, res.status_code) if res.status_code != requests.codes.ok: return num = json.loads(res.text)['num'] print('num=' + str(num)) if num == 1: print('圖形驗證碼輸入錯誤') return url = 'https://www.einvoice.nat.gov.tw/Login' payload = { 'userType': 'N', 'loginType': 'U', 'loginWay': 'W', 'serviceType': 'I', 'serial': '', 'pincode': pw, 'signatur': '', 'ban': '', 'userID': '', 'password': pw, 'pid': '', 'orgType': '', 'bindata': '', 'typeCheck': '1', 'mobile': phone, 'urlMethod': '', 'loginStatus': 'U', 'userTypeStatus': 'N', 'recaptchaValue': captcha, 'g-recaptcha-response': '', 'rclink': '' } headers = { 'User-Agent': user_agent, 'Host': "www.einvoice.nat.gov.tw", 'Referer': "https://einvoice.nat.gov.tw/index", 'Origin': "https://www.einvoice.nat.gov.tw" } res = session.post(url, data=payload, headers=headers) print('登入', url, res.status_code) if res.status_code != requests.codes.ok: return with open('BTC508W.html', 'wb') as file: file.write(res.content) soup = BeautifulSoup(res.text, 'html.parser') print('title:', soup.title.string) startIndex = res.text.index('userInternal.setUser(') + 21 endIndex = res.text.index(');', startIndex) data = res.text[startIndex:endIndex] print('手機條碼:', json.loads(data)['carrierId2'])
if __name__ == "__main__": login()
|