[英]Scrapy CrawlSpider not scraping anything
這是我第一次嘗試使用CrawlSpider
抓取網站,但令我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()
為什么我的蜘蛛不刮任何東西? 我在代碼中做錯了什么嗎? 有人可以幫我嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.