您好,我在 Streamlit 界面的帮助下创建了一个 Selenium 应用程序,它可以根据用户的需要进行交互和更改。该应用程序本身工作得很好,没有任何问题,此外,所有需要安装的软件包都添加到
requirements.txt
文件中。所以根本没有包裹问题。
问题是当我在 selenium 或任何其他网络服务器(例如 GitHub Codespace)上上传应用程序时,该应用程序无法运行。错误是由这一行生成的:
driver = webdriver.Chrome()
我尝试下载 chrome 驱动程序并将其放入工作目录中并使用
external_path = "./chromedriver.exe"
参数,但没有运气。我愿意接受任何建议,谢谢。
if st.button("Scrape Data"):
driver = webdriver.Chrome()
driver.get(f'https://www.airbnb.com{"" if location == f"I{comma}m flexible" else f"/s/{location}"}/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&price_filter_input_type=0&channel=EXPLORE&date_picker_type=calendar&source=structured_search_input_header&search_type=filter_change&price_filter_num_nights=6&checkin={check_in_date}&checkout={check_out_date}{f"&adults={adults_count}" if adults_count > 0 else ""}{f"&children={children_count}" if children_count > 0 else ""}{f"&infants={infants_count}" if infants_count > 0 else ""}{f"&pets={pets_count}" if pets_count > 0 else ""}&flexible_trip_lengths%5B%5D={"one_week" if additional_search_filters == False else stay}{f"&max_price={max_price}" if additional_search_filters else ""}{f"&min_price={min_price}" if additional_search_filters and min_price > 10 else ""}')
titles = driver.find_elements(By.CSS_SELECTOR, 'div.t1jojoys.atm_g3_1kw7nm4.atm_ks_15vqwwr.atm_sq_1l2sidv.atm_9s_cj1kg8.atm_6w_1e54zos.atm_fy_1vgr820.atm_7l_18pqv07.atm_cs_qo5vgd.atm_w4_1eetg7c.atm_ks_zryt35__1rgatj2.dir.dir-ltr')
subtitles = driver.find_elements(By.CSS_SELECTOR, 'span.t6mzqp7.atm_g3_1kw7nm4.atm_ks_15vqwwr.atm_sq_1l2sidv.atm_9s_cj1kg8.atm_6w_1e54zos.atm_fy_kb7nvz.atm_7l_12u4tyr.atm_am_qk3dho.atm_ks_zryt35__1rgatj2.dir.dir-ltr')
prices = driver.find_elements(By.CSS_SELECTOR, 'span._tyxjp1')
ratings = driver.find_elements(By.CSS_SELECTOR, 'span.r1dxllyb.atm_7l_18pqv07.atm_cp_1ts48j8.dir.dir-ltr')
links = driver.find_elements(By.CSS_SELECTOR, 'a.l1ovpqvx.atm_1y33qqm_1ggndnn_10saat9.atm_17zvjtw_zk357r_10saat9.atm_w3cb4q_il40rs_10saat9.atm_1cumors_fps5y7_10saat9.atm_52zhnh_1s82m0i_10saat9.atm_jiyzzr_1d07xhn_10saat9.bn2bl2p.atm_5j_8todto.atm_9s_1ulexfb.atm_e2_1osqo2v.atm_fq_idpfg4.atm_mk_stnw88.atm_tk_idpfg4.atm_vy_1osqo2v.atm_26_1j28jx2.atm_3f_glywfm.atm_kd_glywfm.atm_3f_glywfm_jo46a5.atm_l8_idpfg4_jo46a5.atm_gi_idpfg4_jo46a5.atm_3f_glywfm_1icshfk.atm_kd_glywfm_19774hq.atm_uc_x37zl0_1w3cfyq_oggzyc.atm_70_thabx4_1w3cfyq_oggzyc.atm_uc_glywfm_1w3cfyq_pynvjw.atm_uc_x37zl0_18zk5v0_oggzyc.atm_70_thabx4_18zk5v0_oggzyc.atm_uc_glywfm_18zk5v0_pynvjw.dir.dir-ltr')
for title, subtitle, price, rating, link in zip(titles, subtitles, prices, ratings, links):
if output == "Markdown":
data["Title"].append(title.text)
data["Subtitle"].append(subtitle.text)
data["Price"].append(price.text)
data["Rating"].append(f"⭐{rating.text}")
data["Link"].append(link.get_attribute("href"))
df = pd.DataFrame(data)
首先确保您已经安装了合适版本的ChromeDriver(不带GUI)。然后,尝试像这样配置您的驱动程序:
from selenium.webdriver.chrome.options import Options
# Set Chrome options for headless mode
chrome_options = Options()
chrome_options.add_argument("--headless")
# Initialize Chrome WebDriver with options
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)