Python 网页抓取 : I have a website with pick list. 以及如何提取这些列表中的文本

标签 python selenium web-scraping

链接如下: https://www.doximity.com/sign_ups/9e016f85-d589-4cdf-8240-09c356d4434f/edit?sign_up[user_attributes][firstname]=Jian&sign_up[user_attributes][lastname]=Cui

我需要拉出职业及其对应的专长。 但我的代码只适用于拉动职业。

import requests, bs4

r = requests.get('https://www.doximity.com/sign_ups/9e016f85-d589-4cdf-8240-09c356d4434f/edit?sign_up[user_attributes][firstname]=Jian&sign_up[user_attributes][lastname]=Cui')
soup = bs4.BeautifulSoup(r.text, 'lxml')
spec = soup.find_all('select')

for sub in spec:
    print (sub.text)

请给我一些想法。

最佳答案

检查下面的代码,如有任何问题请告诉我:

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

driver = webdriver.Chrome()
url = 'https://www.doximity.com/sign_ups/9e016f85-d589-4cdf-8240-09c356d4434f/edit?sign_up[user_attributes][firstname]=Jian&sign_up[user_attributes][lastname]=Cui'

driver.get(url)
spec = driver.find_element_by_id("sign_up_user_attributes_credential_id")
for sub in spec.find_elements_by_xpath('./option | ./optgroup/option'):
    if sub.get_attribute('value') != '':
        print(sub.text)
    selected_spec = Select(driver.find_element_by_id("sign_up_user_attributes_credential_id"))
    selected_spec.select_by_visible_text(sub.text)
    time.sleep(0.5)
    occup = driver.find_element_by_xpath('//select[@id="sign_up_user_attributes_user_professional_detail_attributes_specialty_id"]')
    for oc in occup.find_elements_by_xpath('./option'):
        if oc.text != '' and oc.get_attribute('value') != '':
            print(oc.text)

关于Python 网页抓取 : I have a website with pick list. 以及如何提取这些列表中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42170243/

相关文章:

python - 抓取下拉列表提取值的所有组合

python - 如何使用 active/selected 类抓取 <li> 标签?

python - 如何停止 Python 中的短路?

python - 如何在使用selenium时每次在scrapy xpath中更新新的页面源?

python - 使用python抓取jsp网页内容

javascript - 无法在 javascript 中使用 webdriver 打开 chrome

javascript - 使用 Protractor 进行 e2e 测试的 Google 优化

python - 比较 df 中的上一个\下一个值并根据条件进行修正

Python 正则表达式灾难性回溯

python - 在 opencv 3.x 中设置 FPS