2016-07-16 46 views
-1

我用硒刮去angel.co数据,但仍然没有得到的数据从网站无法使用硒

from scrapy import Request,Spider 

import urllib 
from selenium import webdriver 

class AngelSpider(Spider): 
    name = "angel" 
    allowed_domains = ["angel.co"] 
    AJAXCRAWL_ENABLED = True 
    start_urls = (
     "https://angel.co/companies?locations[]=India", 
    ) 

    def __init__(self): 
     self.path ='/usr/lib/chromium-browser/chromedriver' 
     self.driver = webdriver.Chrome(self.path) 

    def parse(self,response): 
     self.driver.get(response.url) 
     self.driver.implicitly_wait(50) 
     while True: 
      next = self.driver.find_element_by_css_selector("div.more") 
      try: 
       next.click() 
       self.driver.implicitly_wait(10) 
       divs = self.driver.find_element_by_xpath("//div[@class= 'results']") 
       for div in divs: 
        name =divs.find_element_by_css_selector("div.name") 
        print name.text 
      except: 
       break 

回答

0

你什么也看不到印刷的原因是,您使用的是裸除了抽取数据条款和基本上默默地忽略所有提出的异常

的问题是在你发现页面上的元素的方式,在这条线,你是因为你正在使用find_element_by_xpath()方法定位一个div元素:

divs = self.driver.find_element_by_xpath("//div[@class= 'results']") 

divsWebElement实例现在这没有可迭代,遍历它会失败的下一行:

for div in divs: 

相反,你在忙什么是这样的:

results = self.driver.find_elements_by_css_selector(".results > div") 
for result in results: 
    name = result.find_element_by_css_selector(".name") 
    print(name.text) 
+0

仍无法刮@alecxe用于'VAL = response.xpath( “// DIV [@数据_tn = '公司/结果']”) COMPANY_NAME = val.xpath(“// DIV [提取()'这两个提取器以及这些xpaths在铬上正常工作,但没有给我的蜘蛛任何结果 –

+0

无法弄清楚我在做什么错误。抓取后没有获得数据,所有的xpath都没问题。 –