2017-02-10 43 views
1

链接低于: https://www.doximity.com/sign_ups/9e016f85-d589-4cdf-8240-09c356d4434f/edit?sign_up[user_attributes][firstname]=Jian&sign_up[user_attributes][lastname]=CuiPython网站抓取:我有一个网站与选择列表。而如何拉文本在这些列表中

我需要拉占领及其相应的专业。 但我的代码只适用于拉职业。

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) 

请给我一些想法。

+0

您将需要硒这一点。 BeautifulSoup不适用于动态网站互动,这就是这种情况,即你必须选择一个职业来获得它的特色 –

+0

陷阱。我会试一试 –

回答

1

检查下面的代码,让我知道在任何问题时:

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) 
+0

非常感谢。它工作完美。 –

相关问题