我想问一个关于使用selenium/selenium基将网站上的信息抓取到 Excel 工作表的过程的问题。几天来我一直在研究 selenium 和 selenium base,我正处于要开始将数据点废弃到 excel 表中的地步。到目前为止,这是我的代码:
import selenium from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import time import os from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.common.keys import Keys from seleniumbase import BaseCase from seleniumbase import js_utils import pandas as pd s=Service('/Users/[name]/Desktop/chromedriver') chromeOptions = Options() chromeOptions.headless = False driver = webdriver.Chrome(service=s, options=chromeOptions) class RecorderTest(BaseCase): def test_recording(self): self.open("https://www.finestrahealth.com/") self.assert_exact_text("finestra", 'a[href="/"] span') self.type('input[placeholder="Procedure"]', "mri") self.type('input[placeholder="Zip code"]', "60007") self.type('input[placeholder="Insurance"]', "atena") self.click('button:contains("Search")') self.assert_element('main p:contains("Hospitals")') out_pocket_price = driver.find_element(By.CLASS_NAME, "text-[40px] text-[#2962FF] leading-[58px] mb-3 tracking-tight smooth") print("out of pocket price:" + out_pocket_price) self.sleep(20) if __name__ == "__main__": from pytest import main main([__file__])
首先,out_pocket_price 的编码不正确,因此我希望对此有所了解。不过,假设这部分已经解决,我将如何获取这个值并将其放入 Excel 工作表以及其他数据点中?该网站有点奇怪,因为 HTML 代码没有 VALUE 属性,就像大多数教程建议的那样。
看起来你有多个驱动程序,还有很多额外的代码,而且对于自付费用来说不是一个好的选择器。这是一个可以执行所有操作、打印价格并将结果输出到 CSV 文件的脚本,这是在 Excel 中轻松打开内容的最简单方法:
import codecs import os from seleniumbase import BaseCase class MedicalTest(BaseCase): def test_medical_list(self): self.open("https://www.finestrahealth.com/") self.assert_exact_text("finestra", 'a[href="/"] span') self.type('input[placeholder="Procedure"]', "mri") self.type('input[placeholder="Zip code"]', "02142") self.type('input[placeholder="Insurance"]', "aetna") self.click('button:contains("Search")') self.assert_element('main p:contains("Hospitals")') self.wait_for_element("div.items-center div.flex") data_to_save = [] print("\nFound prices:") for item in self.find_elements("div.text-white"): print(item.text) data_to_save.append(item.text) file_name = os.path.join(".", "data_file.csv") data_file = codecs.open(file_name, "w+", "utf-8") data_file.writelines("\r\n".join(data_to_save)) data_file.close() print("Data saved to data_file.csv") self.sleep(2) if __name__ == "__main__": # Use "python" to call "pytest" from pytest import main main([__file__, "-s"])
这是我运行它得到的控制台输出:
Found prices: $44934 $27139 $17134 $30009 $11 Data saved to data_file.csv
(因为有print()语句,pytest默认情况下捕获,运行 with pytest -s,除非你已经有一个pytest.ini包含该配置集的文件。)
print()
pytest
pytest -s
pytest.ini