2017-01-09 9 views
1

私はPythonとSeleniumを使用して、特定の検索ページの結果ページからすべてのリンクを削り取ろうとしています。 前の画面で検索した内容に関係なく、結果ページでの検索のURLは "https://chem.nlm.nih.gov/chemidplus/ProxyServlet" です.Seleniumを使用して自動検索した場合、このURLをBeautifulSoupに読み込むと、HTTPError:HTTP Error 404:ここにURLでBeautifulSoupのWebページを特定できません

が見つかりませんが、私のコードです:

from selenium import webdriver 
from selenium.webdriver.support.ui import Select 
from selenium.webdriver.common.by import By 
from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import csv 


# create a new Firefox session 
driver = webdriver.Firefox() 
# wait 3 seconds for the page to load 
driver.implicitly_wait(3) 

# navigate to ChemIDPlus Website 
driver.get("https://chem.nlm.nih.gov/chemidplus/") 
#implicit wait 10 seconds for drop-down menu to load 
driver.implicitly_wait(10) 

#open drop-down menu QV7 ("Route:") 
select=Select(driver.find_element_by_name("QV7")) 
#select "inhalation" in QV7 
select.select_by_visible_text("inhalation") 
#identify submit button 

検索= "/ HTML /ボディ/ DIV [2]/DIV/DIV [2]/DIV/DIV [2] /フォーム/ div [1]/div/span/button [1] "

#click submit button 
driver.find_element_by_xpath(search).click() 

#increase the number of results per page 
select=Select(driver.find_element_by_id("selRowsPerPage")) 
select.select_by_visible_text("25") 
#wait 3 seconds 
driver.implicitly_wait(3) 

#identify current search page...HERE IS THE ERROR, I THINK 
url1="https://chem.nlm.nih.gov/chemidplus/ProxyServlet" 
page1=urlopen(url1) 
#read the search page 
soup=BeautifulSoup(page1.content, 'html.parser') 

私はこれがproxyserverと関係があり、PythonはWebサイトを識別するために必要な情報を受け取っていないと考えていますが、これを回避する方法がわかりません。 ありがとうございます!私は、適切な検索ページ識別するための回避策として、新しいURLを識別するためにセレンを使用

答えて

0

: URL1 = driver.current_url 次へ]を、私はコンテンツを取得し、beautifulsoupにそれを供給するために要求を使用していました。 まとめて:

#Added to the top of the script 
import requests 
... 
#identify the current search page with Selenium 
url1=driver.current_url 
#scrape the content of the results page 
r=requests.get(url) 
soup=BeautifulSoup(r.content, 'html.parser') 
... 
関連する問題