将 HTML 表从受密码保护的页面保存到外部 HTML 文件



>我需要从受密码保护的页面中提取整个html表并将其保存到外部html文件中。

<table id="tblControlorStatus" class="dataTable" aria-describedby="" style="">

driver=webdriver.Firefox()
driver.get("http://MYURL")
assert "Page title" in driver.title 
username = driver.find_element_by_id("ctl00_ContentPlaceHolder1_Login1_UserName")
username.clear()
username.send_keys("MYUSER")
password = driver.find_element_by_name("ctl00$ContentPlaceHolder1$Login1$Password")
password.clear()
password.send_keys("MYPASS")
driver.find_element_by_name("ctl00$ContentPlaceHolder1$Login1$LoginButton").click()
driver.find_element_by_link_text("MYLINKTEXT").click()
html=driver.page_source
soup=BeautifulSoup(html,'lxml')
div=soup.select_one("table#tblControlorStatus")
table=pd.read_html(str(div), header = 0)
print(table[0])

这仅将值从表打印到终端

取而代之的是:

table=pd.read_html(str(div), header = 0)
print(table[0])

尝试这样的事情

html_content = str(div)
html_file = open("output.html", "w")
html_file.write(html_content)
html_file.close()

这应该将表写入外部文件。

谢谢....它与这个小修改一起工作:

html_file = open('D://output.html', 'w', encoding="utf-8")
html_file.write(html_content)
html_file.close()'

最新更新