2013-07-30 4 views
1

페이지 매김을 사용하여 테이블의 링크를 긁어 내려고했습니다. 나는 Selenium을 페이지를 통해 반복 할 수 있고 첫 페이지에서 링크를 얻을 수 있지만 두 페이지를 결합하려고하면 마지막 페이지로 돌아가 더 이상 다음 페이지 버튼이 나타나지 않습니다. 프로세스가 멈추고 아무것도 얻지 못합니다.치료 페이징 selenium python

나는 우아하게 단순히 데이터를 CSV로 반환하는 방법을 알 수 없다. 나는 while true: 루프를 사용하기 때문에 나에게 의아해하고있다.

또 다른 질문은 xpath를 사용하여 구문 분석하려는 링크를 타겟팅하는 것과 관련이 있습니다. 링크는 두 개의 다른 tr - 클래스에 보관됩니다. 하나의 세트는 //tr[@class ="resultsY"]이고 다른 하나는 //tr[@class ="resultsW"]입니다. OR 문을 사용하여 모든 링크를 대상으로 지정할 수 있습니까?

하나의 해결책을 찾았습니다. '//tr[@class ="resultsY"] | //tr[@class ="resultsW"]' 때마다 오류가 발생합니다. 여기

<tr class="resultsW"> 
-<td></td> 
-<td> 
----<a href="fdafda"></a>  <----a link i'm after 
-<td> 
-<td></td> 
</tr> 
<tr class="resultsW"> 
-<td></td> 
-<td> 
----<a href="fdafda"></a>  <----a link i'm after 
-<td> 
-<td></td> 
</tr> 

그리고 내 scrapy입니다 :

import time 
from scrapy.item import Item, Field 
from selenium import webdriver 
from scrapy.spider import BaseSpider 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import Select 
from selenium.common.exceptions import NoSuchElementException 
from scrapy.selector import HtmlXPathSelector 

class ElyseAvenueItem(Item): 
    link = Field() 
    link2 = Field() 

class ElyseAvenueSpider(BaseSpider): 
    name = "s1" 
    allowed_domains = ["nces.ed.gov"] 
    start_urls = [ 
    'https://nces.ed.gov/collegenavigator/'] 

    def __init__(self): 
     self.driver = webdriver.Firefox() 

    def parse(self, response): 
     self.driver.get(response.url) 
     select = Select(self.driver.find_element_by_id("ctl00_cphCollegeNavBody_ucSearchMain_ucMapMain_lstState")) 
     select.deselect_by_visible_text("No Preference") 
     select.select_by_visible_text("Alabama") 
     self.driver.find_element_by_id("ctl00_cphCollegeNavBody_ucSearchMain_btnSearch").click() 

#here is the while loop. it gets to the end of the table and says...no more "next page" and gives me the middle finger 

     '''while True: 
      el1 = self.driver.find_element_by_partial_link_text("Next Page") 
      if el1: 
       el1.click() 
      else: 
       #return(items) 
       self.driver.close()''' 
     hxs = HtmlXPathSelector(response) 

     ''' 
#here i tried: titles = self.driver.find_elements_by_xpath('//tr[@class ="resultsW"] | //tr[@class ="resultsY"]') and i got an error saying that 

     titles = self.driver.find_elements_by_xpath('//tr[@class ="resultsW"]') 
     items = [] 
     for titles in titles: 
      item = ElyseAvenueItem() 

#here i'd like to be able to target all of the hrefs...not sure how 

      link = titles.find_element_by_xpath('//tr[@class ="resultsW"]/td[2]/a') 
      item ["link"] = link.get_attribute('href') 
      items.append(item) 
     yield(items) 

답변

1

좋은 답변을 얻기의 기회를 증가 할 것 세 가지 게시물에이 게시물 속보가

다음은 HTML 테이블입니다.

첫 번째 질문의 경우 "프로세스가 중단되고 아무 것도 얻지 못합니다"라는 의미를보다 정확하게 파악하는 것이 도움이됩니다. 나는 당신이 파일에 "링크"를 쓰려고하는 것을 보지 못한다. 나는 왜 당신이 당신의 else 절에서 당신이하는 일을하는지 이해하지 못합니다.

두 번째 질문에 정규 표현식을 사용하면 트릭을 수행 할 수 있습니다. this을 참조하십시오. 당신이하려고하는 모든가있는 경우 세 번째 질문에 대한

, 요소 title 목록

titles = self.driver.find_elements_by_xpath('//tr[@class ="resultsW"]')이기 때문에,

당신은 단지 옆으로

hrefs=[] 
for titles in titles: 
    href = titles.find_element_by_xpath('a').get_attribute('href') 
    hrefs.append(href) 

는 할 수 페이지에서 링크를 가져 오려면 mechanize, lxml.html 및 | BeautifulSoup을 고려하십시오.