【问题标题】:Selenium Python, parsing through website, opening a new tab, and scrapingSelenium Python,通过网站解析,打开新标签,抓取
【发布时间】:2021-02-11 13:25:27
【问题描述】:

我是 Python 和 Selenium 的新手。我正在尝试做一些事情——我确信我正在以一种非常迂回的方式进行——非常感谢任何帮助。

我试图解析的页面有不同的卡片需要点击,我需要转到每张卡片,然后从那里获取名称 (h1) 和 url。我还没有走多远,这就是我目前所拥有的。

我浏览了第一页,抓取了所有的 url,将它们添加到一个列表中。然后我想浏览列表,然后转到每个 url(打开一个新选项卡),然后从那里获取 h1 和 url。似乎我什至无法抓住 h1,它会打开一个新标签,然后挂起,然后打开同一个标签。

提前谢谢你!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()

driver.get('https://zdb.pedaily.cn/enterprise//') #main URL

title_links = driver.find_elements_by_css_selector('ul.n4 a')
urls = [] #list of URLs

# main = driver.find_elements_by_id('enterprise-list')

for item in title_links:
    urls.append(item.get_attribute('href'))

# print(urls)

for url in urls:
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get(url)
    print(driver.find_element_by_css_selector('div.info h1'))

【问题讨论】:

    标签: python selenium parsing web-scraping


    【解决方案1】:

    好吧,这里有几个问题:

    • 您应该更具体地使用您的标签来获取网址。这会导致相同 url 的多个副本——这就是它再次打开相同页面的原因。
    • 在尝试抓取物体之前,您应该给网站足够的时间来加载,这可能是它超时的原因,但在抓取物体之前始终保持安全。
    • 您必须将焦点移回原始页面才能继续迭代列表
    • 你不需要注入JS来打开一个新标签并使用py调用打开,并且JS格式可以更干净
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.Chrome()
    driver.get('https://zdb.pedaily.cn/enterprise/')  # main URL
    
    # Be much more specific or you'll get multiple returns of the same link
    urls = driver.find_elements(By.TAG_NAME, 'ul.n4 li div.img a')
    
    for url in urls:
        # get href to print
        print(url.get_attribute('href'))
        # Inject JS to open new tab
        driver.execute_script("window.open(arguments[0])", url)
        # Switch focus to new tab
        driver.switch_to.window(driver.window_handles[1])
        # Make sure what we want has time to load and exists before trying to grab it
        WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.info h1')))
        # Grab it and print it's contents
        print(driver.find_element(By.CSS_SELECTOR, 'div.info h1').text)
        # Uncomment the next line to do one tab at a time. Will reduce speed but not use so much ram.
        #driver.close()
        # Focus back on first window
        driver.switch_to.window(driver.window_handles[0])
    # Close window
    driver.quit()
    

    【讨论】:

    • 非常感谢!!我花了很多时间来调整它,并四处寻找!我真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 2015-04-10
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多