2017-02-10 5 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 web scraping:私はpick listを持つウェブサイトを持っています。そして、どのようにリンクが以下であるこれらのリスト

は、私が占領し、それに対応する専門を引っ張っする必要があります。 しかし、私のコードは職業を引き上げるだけで動作します。

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

Gotcha。私は試してみる –

答えて

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

ありがとう、ありがとう。それは完璧に働いた。 –

関連する問題