使用Splinter&Selenium自动化浏览器时Facebook Marketplace无法动态加载
Facebook Marketplace滚动无法加载更多商品的问题
我尝试使用Splinter和Selenium自动化浏览Facebook Marketplace,但遇到页面滚动时无法动态加载更多结果的问题,尽管脚本已模拟滚动行为。
from bs4 import BeautifulSoup as soup import time from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options try: # Set up Chrome options to modify the user-agent and maximize the window chrome_options = Options() chrome_options.add_argument("--start-maximized") # Maximize the window chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") # Custom user-agent # Initialize the browser with the modified options browser = Browser('chrome', headless=False, executable_path="C:\\chromedriver-win64\\chromedriver.exe", options=chrome_options) # Facebook login page login_url = "https://www.facebook.com/login" browser.visit(login_url) # Enter login credentials WebDriverWait(browser.driver, 10).until( EC.presence_of_element_located((By.ID, "email")) ).send_keys("your_email") # Replace with your Facebook email WebDriverWait(browser.driver, 10).until( EC.presence_of_element_located((By.ID, "pass")) ).send_keys("your_password") # Replace with your Facebook password # Click the login button WebDriverWait(browser.driver, 10).until( EC.element_to_be_clickable((By.NAME, "login")) ).click() # Wait for redirection to the marketplace time.sleep(5) # Visit the Facebook Marketplace page url = "https://www.facebook.com/marketplace/orlando/search?daysSinceListed=1&sortBy=creation_time_descend&query=iphone%2013&exact=false" browser.visit(url) # Close any pop-ups if they appear try: close_button = WebDriverWait(browser.driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[aria-label="Cerrar"]')) ) close_button.click() except Exception: print("No pop-up to close.") # Scroll down the page to load more items scroll_count = 4 scroll_delay = 2 for _ in range(scroll_count): browser.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(scroll_delay) # Parsing the page content html = browser.html html_soup = soup(html, 'html.parser') # Find all product elements product_elements = html_soup.find_all('div', class_="x9f619 x78zum5 x1r8uery xdt5ytf x1iyjqo2 xs83m0k x1e558r4 x150jy0e x1iorvi4 xjkvuk6 xnpuxes x291uyu x1uepa24") # Extract and print product titles, prices, and locations for product in product_elements: title_element = product.find('span', class_="x1lliihq x6ikm8r x10wlt62 x1n2onr6") product_title = title_element.get_text(strip=True) if title_element else "Title not found" price_element = product.find('span', class_="x193iq5w xeuugli x13faqbe x1vvkbs x1xmvt09 x1lliihq x1s928wv xhkezso x1gmr53x x1cpjm7i x1fgarty x1943h6x xudqn12 x676frb x1lkfr7t x1lbecb7 x1s688f xzsf02u") product_price = price_element.get_text(strip=True) if price_element else "Price not listed" location_element = product.find('span', class_="x1lliihq x6ikm8r x10wlt62 x1n2onr6 xlyipyv xuxw1ft x1j85h84") product_location = location_element.get_text(strip=True) if location_element else "Location not listed" # Print extracted information print(f"Title: {product_title}") print(f"Price: {product_price}") print(f"Location: {product_location}") print("-" * 50) except Exception as e: print(f"An error occurred: {e}") finally: browser.quit()
已尝试的解决方法:
- 最大化浏览器窗口并设置自定义user-agent字符串
- 在滚动后添加延迟以等待结果加载
- 手动启动浏览器并滚动,同样无法触发动态加载
核心问题:脚本运行正常且执行了滚动操作,但Facebook Marketplace仍无法动态加载更多商品,页面未更新新内容。
内容的提问来源于stack exchange,提问作者asdfdffdsa
相关产品推荐
相关产品推荐

