python爬虫selenium之选择下拉列表的两种方式

本篇博文仍以https://www.17sucai.com/pins/demo-show?id=5926,这个网址示例如何进行下拉列表的选项选择。
选择下拉列表的时候,存在两种情况。第一种是有select标签的,这种情况下可以通过from selenium.webdriver.support.ui import Select方式实现,如图片情形。
在这里插入图片描述
但也存在没有select标签的下拉列表,这时候就需要我们手动链接到该位置。如图片情形所示,就是a标签,不是select标签,无法通过from selenium.webdriver.support.ui import Select方式实现
在这里插入图片描述
下面我们分别来讲第一个情形和第二个情形的操作方法。

  • 第一个情形
'''
第一种情形:可以通过 from selenium.webdriver.support.ui import Select
'''

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换ifrane
driver.switch_to_frame(driver.find_element_by_id('iframe'))
# 找到下拉框
selectTag = Select(driver.find_element_by_name('country-wrap'))  # select标签
# 获得选择项
# 1.根据值来选择
selectTag.select_by_value('CA')
# 2.根据索引来选择
# selectTag.select_by_index(3)
  • 第二个情形
'''
第二种情形:手动点击
'''

from selenium import webdriver
from selenium.webdriver.support.ui import Select
# 输入选择项
key = int(input('请输入选择内容:'))

driver = webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换ifrane
driver.switch_to_frame(driver.find_element_by_id('iframe'))
# 找到下拉框
selectTag = driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]').click() # 点击下拉列表位置
# 获得选择项
if key==1:
    driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[1]/a').click()
elif key==2:
    driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[2]/a').click()
  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在爬取下拉列表的时候,我们可以使用Selenium或者BeautifulSoup库。 使用Selenium库的话,我们可以模拟用户在网页上选择下拉列表的过程,具体步骤如下: 1. 安装Selenium库:`pip install selenium` 2. 下载相应的浏览器驱动,例如Chrome浏览器的驱动可以在这里下载:http://chromedriver.chromium.org/downloads 3. 导入Selenium库,并启动浏览器: ```python from selenium import webdriver # 打开Chrome浏览器 browser = webdriver.Chrome('path/to/chromedriver') # 将path/to/chromedriver替换成你的驱动路径 ``` 4. 打开网页,并选择下拉列表: ```python # 打开网页 browser.get('http://example.com') # 找到下拉列表元素并点击 select = browser.find_element_by_xpath('//select[@name="example-select"]') select.click() ``` 5. 选择下拉列表中的选项: ```python # 找到下拉列表选项并点击 option = browser.find_element_by_xpath('//select[@name="example-select"]/option[text()="Option 1"]') option.click() ``` 完整代码如下: ```python from selenium import webdriver # 打开Chrome浏览器 browser = webdriver.Chrome('path/to/chromedriver') # 将path/to/chromedriver替换成你的驱动路径 # 打开网页 browser.get('http://example.com') # 找到下拉列表元素并点击 select = browser.find_element_by_xpath('//select[@name="example-select"]') select.click() # 找到下拉列表选项并点击 option = browser.find_element_by_xpath('//select[@name="example-select"]/option[text()="Option 1"]') option.click() # 关闭浏览器 browser.quit() ``` 如果不想使用Selenium库,也可以使用BeautifulSoup库来解析网页HTML代码,找到下拉列表并获取其中的选项。具体步骤如下: 1. 安装BeautifulSoup库:`pip install beautifulsoup4` 2. 导入BeautifulSoup库,获取网页HTML代码: ```python import requests from bs4 import BeautifulSoup # 获取网页HTML代码 url = 'http://example.com' response = requests.get(url) html = response.text # 解析HTML代码 soup = BeautifulSoup(html, 'html.parser') ``` 3. 找到下拉列表元素并获取选项: ```python # 找到下拉列表元素 select = soup.find('select', {'name': 'example-select'}) # 获取所有选项 options = select.find_all('option') # 获取第一个选项的值 value = options[0].get('value') # 获取第一个选项的文本 text = options[0].text ``` 完整代码如下: ```python import requests from bs4 import BeautifulSoup # 获取网页HTML代码 url = 'http://example.com' response = requests.get(url) html = response.text # 解析HTML代码 soup = BeautifulSoup(html, 'html.parser') # 找到下拉列表元素 select = soup.find('select', {'name': 'example-select'}) # 获取所有选项 options = select.find_all('option') # 获取第一个选项的值 value = options[0].get('value') # 获取第一个选项的文本 text = options[0].text ``` 注意,如果下拉列表是由JavaScript动态生成的,那么BeautifulSoup库可能无法获取到元素,这时候就需要使用Selenium库了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值