Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 痒痒的爬行蜘蛛不刮任何东西_Python_Scrapy_Web Crawler - Fatal编程技术网

Python 痒痒的爬行蜘蛛不刮任何东西

Python 痒痒的爬行蜘蛛不刮任何东西,python,scrapy,web-crawler,Python,Scrapy,Web Crawler,这是我第一次尝试使用CrawlSpider抓取网站,令我悲伤的是,我的爬行器没有返回任何结果。我也是python新手,所以如果我犯了任何明显的错误,请耐心等待 下面是我的代码: from scrapy.settings import Settings from scrapy.settings import default_settings from selenium import webdriver from urlparse import urlparse import csv fr

这是我第一次尝试使用
CrawlSpider
抓取网站,令我悲伤的是,我的爬行器没有返回任何结果。我也是python新手,所以如果我犯了任何明显的错误,请耐心等待

下面是我的代码:

from scrapy.settings import Settings
from scrapy.settings import default_settings 
from selenium import webdriver
from urlparse import urlparse
import csv    
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy import log

default_settings.DEPTH_LIMIT = 3

class MySpider(CrawlSpider):
    def __init__(self,url,ofile,csvWriter):
        self.url=url
        self.driver=webdriver.PhantomJS('/usr/local/bin/phantomjs')
        self.ofile=ofile
        self.csvWriter=csvWriter
        self.name = "jin"
        self.start_urls = [url]
        self.rules = [Rule(SgmlLinkExtractor(), callback='parse_website', follow=True)]

    def parse_website(self,response):
        url=self.url
        driver=self.driver
        csvWriter=self.csvWriter
        ofile=self.ofile
        self.log('A response from %s just arrived!' % response.url)
        driver.get(url)
        htmlSiteUrl = self.get_site_url(driver)
        htmlImagesList=self.get_html_images_list(driver,url)

    def get_site_url(self,driver):
        url = driver.current_url
        return url

    def get_html_images_list(self,driver,url):
        listOfimages = driver.find_elements_by_tag_name('img') 
        return listOfimages
        driver.close()

with open('/Users/hyunjincho/Desktop/BCorp_Websites.csv') as ifile:  
   website_batch= csv.reader(ifile, dialect=csv.excel_tab)  
   ofile=open('/Users/hyunjincho/Desktop/results.csv','wb')
   csvWriter = csv.writer(ofile,delimiter=' ')
   for website in website_batch: 
      url = ''.join(website)         
      aSpider=MySpider(url,ofile,csvWriter)
   ofile.close()

为什么我的蜘蛛不刮东西?我的代码有没有做错什么?有人能帮帮我吗?

你不应该用这种方式启动spider,看看它是如何在优秀的环境中完成的

scrapy crawl jin

此外,如果您希望从外部文件读取url/s,请参阅


最后,如果您希望将它们写入csv文件,请使用configured创建并处理它们,这样就可以完成输出。使用

此代码甚至不应该编译,因为您在定义MySpider之前参考了它(如果这是您的实际缩进)。对不起,我的错误-我的实际代码中的缩进没有错误,我在我原来的帖子中修复了缩进。当你尝试运行你的爬行器时会发生什么?什么都没有发生-没有错误消息,什么都没有。但是在什么都没有得到之后,即使我花了45分钟等待在我的.csv文件上显示抓取的结果,我会说我的代码是错误的。我不认为我的蜘蛛正在抓取我在input.csv文件中指定的网站