\# -\*- coding: utf-8 -\*- import requests import sys import re import io import xlwt import xlrd import time sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding\='utf-8') def getHtml(url): #用requests得到網頁 # 構造頭部 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'} cook={'cookies': 'PHPSESSID=59qav75m8ra9252q83bpoisf11; XSRF-TOKEN=eyJpdiI6InBsSk8zclltYWFzUFBRNmwzK1JiTFE9PSIsInZhbHVlIjoiTjBKbTUrUzVjcGwwbjg0TG1wUHBMMmxhOGQ4TXVhekxBR2JDR1pFRkkzN0FjaFUxbmVcL0phc0s1NUcrM1wvcmtscXVPdkN5Sld1SmFaTTZiMFc0K0VpUT09IiwibWFjIjoiNzcyMGQxZWJmYWFiM2NkODJmNTJiMWE4YTM0MjZmMDVkOTBkNmI5YmFiMmUwNTZjMmIxMGYxNmU5ODU1Nzg2NiJ9; laravel\_session=eyJpdiI6ImJLSjhoY3BlZHUyRHpMWG1xTExmN1E9PSIsInZhbHVlIjoiaTlWZzNJQnZoOE1ydFltSmptUk9sTTRZZDhISWx3UmZZbmEwSmkxMmNZVVliZjUraE9iRVwvSXJSN3Rlck9STk1zVHZxZ1RmcnhFZldvNVRQNnBka1ZBPT0iLCJtYWMiOiJmYjZkNTMyZGFlMzY5Y2IyZmIzNjdiMWNjODY4YTUxY2NhNjlkNzIxYzVkYzg1MjNjMjljOWMzMjE4MzA5M2UzIn0%3D'} time.sleep(0.2) gethtml = requests.get(url, headers\=headers,cookies\=cook) return gethtml.text def biaoName():#表的列表標題 sheet.write(0, 0, "序號") sheet.write(0, 1, "基金名稱") sheet.write(0, 2, "日期") sheet.write(0, 3, "操做") return biaoName url="http://www.zhitouxing.com/my\_strg" resp = getHtml(url) FoundNames = re.findall("<tr id='.\*?' style='' ><td>(.\*?)</td>", resp, re.S) \# 正則表達式 FoundTimes = re.findall("</a></td><td>(.\*?)<br/>", resp, re.S) FoundMakes = re.findall("<span style='color:red;'>今天定投</span></td><td><span>(.\*?)</span>", resp, re.S) workbook = xlwt.Workbook( encoding\="utf-8" ) sheet = workbook.add\_sheet('智投星獲取信息') biaoName() for i in range(0, len(FoundNames), 1): sheet.write(i+1, 0, i+1) sheet.write(i+1, 1, FoundNames\[i\]) sheet.write(i+1, 2, FoundTimes\[i\]) sheet.write(i+1, 3, FoundMakes\[i\]) i=i+ 1 workbook.save(r"C:\\Users\\fuxingyu\\Desktop\\智投星.xls")