페이지 매김을 사용하여 테이블의 링크를 긁어 내려고했습니다. 나는 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)