import requests from bs4 import BeautifulSoup import cx_Oracle # 连接Oracle数据库 conn = cx_Oracle.connect('username/password@hostname:port/sid') # 创建游标 cur = conn.cursor() # 爬取国家法定节假日的网页 url = 'http://sousuo.gov.cn/s.htm?t=paper&advance=false&n=10&timetype=timeqb&mintime=0&maxtime=0&sort=pubtime&sortType=1&searchfield=&pcodeJiguan=&childtype=&subchildtype=&tsbq=&pubtimeyear=&puborg=&q=%E5%9B%BD%E5%AE%B6%E6%B3%95%E5%AE%9A%E8%8A%82%E5%81%87%E6%97%A5' response = requests.get(url) # 解析网页 soup = BeautifulSoup(response.content, 'html.parser') table = soup.find('table', {'class': 'wtable'}) # 提取数据并保存到Oracle数据库 for tr in table.find_all('tr')[1:]: tds = tr.find_all('td') holiday_date = tds[1].get_text().strip() # 节假日日期 holiday_name = tds[2].get_text().strip() # 节假日名称 cur.execute("INSERT INTO HOLIDAYS(HOLIDAY_DATE, HOLIDAY_NAME) VALUES (:1, :2)", (holiday_date, holiday_name)) conn.commit() # 关闭游标和数据库连接 cur.close() conn.close()