gpt4 book ai didi

python-2.7 - Python 网页抓取(Beautiful Soup、Selenium 和 PhantomJS): Only scraping part of full page

转载 作者:行者123 更新时间:2023-12-04 21:10:54 26 4
gpt4 key购买 nike

您好,我在尝试从网站(fantsylabs dotcom)中抓取数据以进行建模时遇到问题。我只是一个黑客,所以请原谅我对 comp sci 术语的无知。我试图完成的是......

  • 使用 selenium 登录网站并导航到包含数据的页面。
    ## Initialize and load the web page
    url = "website url"
    driver = webdriver.Firefox()
    driver.get(url)
    time.sleep(3)

    ## Fill out forms and login to site
    username = driver.find_element_by_name('input')
    password = driver.find_element_by_name('password')
    username.send_keys('username')
    password.send_keys('password')
    login_attempt = driver.find_element_by_class_name("pull-right")
    login_attempt.click()

    ## Find and open the page with the data that I wish to scrape
    link = driver.find_element_by_partial_link_text('Player Models')
    link.click()
    time.sleep(10)

    ##UPDATED CODE TO TRY AND SCROLL DOWN TO LOAD ALL THE DYNAMIC DATA
    scroll = driver.find_element_by_class_name("ag-body-viewport")
    driver.execute_script("arguments[0].scrollIntoView();", scroll)

    ## Try to allow time for the full page to load the lazy way then pass to BeautifulSoup
    time.sleep(10)
    html2 = driver.page_source

    soup = BeautifulSoup(html2, "lxml", from_encoding="utf-8")
    div = soup.find_all('div', {'class':'ag-pinned-cols-container'})
    ## continue to scrape what I want

  • 这个过程的工作原理是它登录,导航到正确的页面,但是一旦页面完成动态加载(30 秒),将它传递给 beautifulsoup。我在表中看到了大约 300 多个我想要抓取的实例......但是 bs4 抓取器只吐出 300 个的大约 30 个实例。从我自己的研究来看,这似乎可能是通过动态加载数据的问题javascript 并且只有推送到 html 的内容才会被 bs4 解析? ( Using Python requests.get to parse html code that does not load at once )

    对于任何提供建议而不在网站上创建配置文件的人来说,可能很难重现我的示例,但是使用 phantomJS 初始化浏览器是否是“抓取”所有实例以捕获所有所需数据所需的全部?
        driver = webdriver.PhantomJS() ##instead of webdriver.Firefox()

    任何想法或经验将不胜感激,因为如果这是我遇到的情况,我从来不必处理动态页面/抓取 javascript。

    在 Alecs 回复后更新:

    下面是目标数据的屏幕截图(以蓝色突出显示)。您可以在图像右侧看到滚动条,它嵌入在页面中。我还提供了此容器中页面源代码的 View 。

    enter image description here

    我修改了我提供的原始代码以尝试向下滚动到底部并完全加载页面,但它无法执行此操作。当我将驱动程序设置为 Firefox() 时,我可以看到页面通过外部滚动条向下移动,但不在目标容器内。我希望这是有道理的。

    再次感谢您的任何建议/指导。

    最佳答案

    这并不容易回答,因为我们没有办法重现这个问题。

    一个问题是lxmlnot handling this specific HTML particularly well您可能需要尝试 changing the parser :

    soup = BeautifulSoup(html2, "html.parser")
    soup = BeautifulSoup(html2, "html5lib")

    此外,在 BeautifulSoup 中可能不需要首先。您可以使用 selenium 定位元素以很多不同的方式。例如,在这种情况下:
    for div in driver.find_elements_by_css_selector(".ag-pinned-cols-container'"):
    # do smth with 'div'

    也可能是页面滚动到底部时动态加载了数据。在这种情况下,您可能需要将页面滚动到底部,直到看到所需的数据量或滚动时不再加载新数据。以下是带有示例解决方案的相关线程:
  • Scrolling web page using selenium python webdriver
  • Scroll down to bottom of infinite page with PhantomJS in Python
  • Slow scrolling down the page using Selenium
  • Stop the Scroll in Dynamic Page with Selenium in Python
  • 关于python-2.7 - Python 网页抓取(Beautiful Soup、Selenium 和 PhantomJS): Only scraping part of full page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769715/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com