简体   繁体   English

如何使用 Selenium-Python 抓取 dropdwon 菜单?

[英]How to scrape a dropdwon menu with Selenium-Python?

I have to scrape the data from this web page: http://www.mlsa.am/?page_id=368.The webpage is in Armenian.我必须从这个 web 页面中抓取数据: http://www.mlsa.am/?page_id=368 。网页是亚美尼亚语。 This is a dropdown menu where the options are: Regions, Areas, Communities, Type of Subsidy, Month and Year.这是一个下拉菜单,其中选项有:地区、地区、社区、补贴类型、月份和年份。 Once these options are selected a table shows up with information on the citizens of these places who get the different kinds of subsidies.一旦选择了这些选项,就会显示一个表格,其中包含有关这些地方获得不同类型补贴的公民的信息。 The difficulty I am facing right now is that the second dropdown (Areas) depends on the option you select on the first dropdown and the third (Communities) depends and what you select on the previous dropdowns.我现在面临的困难是第二个下拉列表(区域)取决于您在第一个下拉列表中的 select 选项,第三个(社区)取决于您在之前下拉列表中的 select 选项。 How should I write my code for this type of web page?我应该如何为这种类型的 web 页面编写代码?

This is how the web page looks like when you inspect it这就是您检查 web 页面时的样子

<!--Մարզեր-->
            <div class="td-pb-row">
                <div class="td-pb-span2"></div>
                <div class="td-pb-span5">
                    Մարզեր <span class="ben-required">*</span> &nbsp;&nbsp;&nbsp;
                    <select  id="ref_regions_id"  name="ref_regions" style="border:1px solid #0790A2;"  >
                        <option value="0" > Ընտրել </option>
                                                <option  value="1"> ԱՐԱԳԱԾՈՏՆ</option>
value="2"> ԱՐԱՐԱՏ</option>

    <option  value="3"> ԱՐՄԱՎԻՐ</option>

    <option  value="4"> ԳԵՂԱՐՔՈՒՆԻՔ</option>

    <option  value="5"> ԼՈՌԻ</option>

    <option  value="6"> ԿՈՏԱՅՔ</option>

    <option  value="7"> ՇԻՐԱԿ</option>

    <option  value="8"> ՍՅՈՒՆԻՔ</option>

    <option  value="9"> ՎԱՅՈՑ ՁՈՐ</option>

    <option  value="10"> ՏԱՎՈՒՇ</option>

    <option  value="11"> ԵՐԵՎԱՆ</option>`

                    </select>
                </div>

I am using selenium with python and so far this is my code:我正在使用 selenium 和 python 到目前为止这是我的代码:

import time
import requests
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
chrome_path = r"C:\Users\ivrav\selenium-2.25.0\Driver\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
print("loading url into browser...")
def get_all_pages():
    payload={'value':'1'}
driver.get("http://www.mlsa.am/?page_id=368")
print(url.text)
time.sleep(2)

To select an option from the dropdown you showed in your HTML, I would use the Select() class in Python:对于 select,您在 HTML 中显示的下拉列表中的一个选项,我将使用 ZA7F5F392326B538241 中的Select() class:

from selenium.webdriver.support.ui import Select


select = Select(driver.find_element_by_id('ref_regions_id'))

Then, you can select an option as such:然后,您可以选择 select ,如下所示:

select.select_by_text("ԱՐՄԱՎԻՐ")

Or, using the value attributes on the option elements:或者,使用选项元素的value属性:

select.select_by_value(0)

Lastly, you can get all available options in the dropdown:最后,您可以在下拉列表中获取所有可用选项:

options = select.options

for option in options:
    print(option)

To work with each dropdown depending on the previous dropdown, you'll just need to select an option for each dropdown in the correct order against a pre-determined set of options.要根据前一个下拉菜单使用每个下拉菜单,您只需要针对预先确定的一组选项以正确的顺序为每个下拉菜单添加一个选项。 Each dropdown has a unique ID, so that should help.每个下拉菜单都有一个唯一的 ID,所以这应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM