import requests #引用requests import os #os from bs4 import BeautifulSoup # 从bs4引入BeautifulSoup import csv import codecs import pymysql # 连接数据库 connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='root', db='ess', charset='utf8' ) # 获取游标 cursor = connect.cursor() lst=[] headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36' } url='http://datachart.500.com/dlt/history/newinc/history.php?start=19091&end=21120' r = requests.get(url, headers=headers) r.encoding='utf-8' text=r.text soup = BeautifulSoup(text, "html.parser") tbody=soup.find('tbody',id="tdata") tr=tbody.find_all('tr') lens = len(tr) tda=tr[0].find_all('td') for page in range(0,lens): td=tr[page].find_all('td') #存数据库 try: sql = "INSERT INTO caipiao(qihao,num1,num2,num3,num4,num5,te1,te2) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')" data = (td[0].text,td[1].text,td[2].text,td[3].text,td[4].text,td[5].text,td[6].text,td[7].text) # self.cursor.execute(sql % data) # self.connect.commit() cursor.execute(sql % data) connect.commit() print('成功插入', cursor.rowcount, '条数据') except Exception as e: #print('数据插入错误') print('程序出错',e) #存csv excel lst.append([td[0].text,td[1].text,td[2].text,td[3].text,td[4].text,td[5].text,td[6].text,td[7].text]) with open("Lottery_data.csv",'w') as csvfile: writer = csv.writer(csvfile) writer.writerow(['期号','号码1', '号码2', '号码3', '号码4', '号码5', '号码6', '号码7']) writer.writerows(lst) csvfile.close() print('succes')