https://insuretechconnect.com/speakers/

您好,我想从上面的网站中提取演讲者的信息,我,python,selenium-webdriver,request,css-selectors,webdriverwait"/>

首页 > 解决方案 > 如何在 python 中使用 Selenium 或请求提取

https://insuretechconnect.com/speakers/

您好,我想从上面的网站中提取演讲者的信息,我

问题描述

https://insuretechconnect.com/speakers/

您好,我想从上面的网站中提取演讲者的信息,我想要他们的姓名、职务、公司、img src 链接和描述。

在此处输入图像描述

但是,我的代码只能提取姓名、职务和公司。

driver = webdriver.Chrome(r'XXX\chromedriver.exe')
driver.get('https://insuretechconnect.com/speakers/')

speakers_info=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.awsm-personal-info'))) speakers_info_fulllist = [] for e in speakers_info: speakers_info_fulllist.append(e.text.split('\n'))

有没有更好的方法来为所有发言者(Selenium 或 Request)提取信息?

提前致谢。


如何以编程方式在约束布局上设置具有 Autosized textType 的 textView?

大家好,我是一个该死的初学者,需要一些帮助

我必须在 Android Studio 的编辑器上构建一个带有 5 个文本视图的 ContraintLAyout,现在我需要动态地使用这个 ContraintLayout 42 次。因此,我搜索了一段时间以寻找解决方案,并找到了可行的方法。但结果不是预期的。

<TextView
                android:id="@+id/textView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:gravity="center"
                android:text="MO"
                app:autoSizeTextType="uniform"
                app:layout_constraintBottom_toTopOf="@+id/textView4"
                app:layout_constraintEnd_toStartOf="@+id/textView2"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent="0.34"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent="0.34" />

如何在 Java 代码上设置这些值?

我尝试过这样的事情

GridLayout gridLayout = findViewById(R.id.kalenderfeldGL);
        ConstraintLayout tagesfeld = (ConstraintLayout) gridLayout.getChildAt(0);
        tagesfeld.setId(View.generateViewId());
        TextView tagname = new TextView(this);
        tagname.setId(View.generateViewId());
        //tagname.setHeight(0);
        //tagname.setWidth(0);
        ConstraintLayout.LayoutParams cltagesfele = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT, ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_PERCENT);
        tagname.setLayoutParams(cltagesfele);
        //tagname.setText("MO");
        TextViewCompat.setAutoSizeTextTypeWithDefaults(tagname, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
        tagname.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        //tagname.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);



        //tagname.setText("MO");
        tagesfeld.addView(tagname);
        ConstraintSet cstagname = new ConstraintSet();
        //cstagname.clone(tagesfeld);
        cstagname.connect(tagname.getId(), ConstraintSet.TOP, tagesfeld.getId(), ConstraintSet.TOP, 0);
        cstagname.connect(tagname.getId(), ConstraintSet.START, tagesfeld.getId(), ConstraintSet.START, 0);
        cstagname.constrainPercentHeight(tagname.getId(), 0.34F);
        cstagname.constrainPercentWidth(tagname.getId(), 0.34F);
        cstagname.applyTo(tagesfeld);
        tagname.setText("MO");

我想要左上角的“MO”字母这行得通,但字母不是并排的,它们处于垂直位置,并且 O 被切割在下侧。

标签: pythonselenium-webdriverrequestcss-selectorswebdriverwait

解决方案


要仅使用Selenium提取演讲者Eran Agrios的信息,即TitleCompanyImage LinkDescription ,您可以使用以下Locator Strategies

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    
    driver.get("https://insuretechconnect.com/speakers/")
    driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='awsm-personal-info']//h3[contains(., 'Eran')]//preceding::img[1]"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='awsm-personal-info']//h3[contains(., 'Eran')]//preceding::img[1]"))).click()
    print("Title is : "+ WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='awsm-modal-content-inner']/h2[contains(., 'Eran')]//following::h3[1]"))).get_attribute("innerHTML"))
    print("Company is : "+ WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='awsm-modal-content-inner']/h2[contains(., 'Eran')]//following::h3[2]/b"))).get_attribute("innerHTML"))
    print("Image Link is : "+ WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='awsm-modal-content-inner']/h2[contains(., 'Eran')]//preceding::img[1]"))).get_attribute("src"))
    print("Description : "+ WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='awsm-modal-content-inner']/h2[contains(., 'Eran')]//following::p[1]"))).get_attribute("innerHTML"))
    driver.quit()
    
  • 控制台输出:

    Title is : Head of Global Go To Market, Financial Services Cloud and Wealth &amp; Asset Management
    Company is : Salesforce
    Image Link is : https://n68y02w29js2mtetnvfd871d-wpengine.netdna-ssl.com/wp-content/uploads/2019/07/Eran-Agrois-500x500.png
    Description : Eran Agrios leads the Go To Market strategy for Financial Services Cloud at Salesforce. Eran has over 15 years of experience in customer relationship management technology. She has spent the last 10 years at Salesforce working with Financial Services companies on innovation and digital transformation. Most recently, her focus has been on the success of Salesforce’s first industry product, Financial Services Cloud.
    

推荐阅读