2017-01-09 1 views
1

Я использую Python и Selenium, чтобы попытаться очистить все ссылки с страницы результатов определенной страницы поиска. Независимо от того, что я ищу на предыдущем экране, URL-адрес для любого поиска на странице результатов: «https://chem.nlm.nih.gov/chemidplus/ProxyServlet» Если я использую Selenium для автопоиска, попробуйте прочитать этот URL-адрес в BeautifulSoup, я получаю HTTPError: HTTP Error 404: Не найденНевозможно идентифицировать веб-страницу в BeautifulSoup по URL

Вот мой код:

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/тело/дела [2]/дела/дела [2]/дела/дела [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 не получает необходимую информацию для идентификации веб-сайта, но я не уверен, как обойти это. Спасибо заранее!

ответ

0

Я использовал Селен для идентификации нового URL в качестве обходного для идентификации правильного поиска страницы: 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') 
... 
Смежные вопросы