练习用pandas获取网页表格数据并保存至excel中遇到问题
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'http://jntj.jinan.gov.cn/art/2022/9/6/art_18279_4747210.html'
head = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36','Connection': 'keep-alive'}
html = requests.get(url,headers=head)
html.encoding='utf_8_sig'
soup = BeautifulSoup(html.text,'lxml')
soup = str(soup)
html_data = pd.read_html(soup)
table_data = pd.DataFrame()
print(html_data)
for i in html_data:
table_data= table_data.append(i)
# print(table_data)
table_data.to_excel('table_.xlsx')
保存到表格后会出现两遍数据
我的解答思路和尝试过的方法
怎么修改能够保存一遍数据,并且能将表格标题同时写入