python爬虫问题求教,程序能够运行,但不能输出到文件,甚至都没有新建文本文件,请大神指教
2019-02-24 18:46:23 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: weather)
2019-02-24 18:46:23 [scrapy.utils.log] INFO: Versions: lxml 4.3.1.0, libxml2 2.9.5, cssselect 1.0.3, parsel 1.5.1, w3lib 1.20.0, Twisted 18.9.0, Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.0.0 (OpenSSL 1.1.1a 20 Nov 2018), cryptography 2.5, Platform Windows-10-10.0.17134-SP0
2019-02-24 18:46:23 [scrapy.crawler] INFO: Overridden settings: {‘BOT_NAME’: ‘weather’, ‘NEWSPIDER_MODULE’: ‘weather.spiders’, ‘SPIDER_MODULES’: [‘weather.spiders’]}
2019-02-24 18:46:23 [scrapy.extensions.telnet] INFO: Telnet Password: dbc9ded506154659
2019-02-24 18:46:24 [scrapy.middleware] INFO: Enabled extensions:
[‘scrapy.extensions.corestats.CoreStats’,
‘scrapy.extensions.telnet.TelnetConsole’,
‘scrapy.extensions.logstats.LogStats’]
2019-02-24 18:46:24 [scrapy.middleware] INFO: Enabled downloader middlewares:
[‘scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware’,
‘scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware’,
‘scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware’,
‘scrapy.downloadermiddlewares.retry.RetryMiddleware’,
‘scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware’,
‘scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware’,
‘scrapy.downloadermiddlewares.redirect.RedirectMiddleware’,
‘scrapy.downloadermiddlewares.cookies.CookiesMiddleware’,
‘scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware’,
‘scrapy.downloadermiddlewares.stats.DownloaderStats’]
2019-02-24 18:46:24 [scrapy.middleware] INFO: Enabled spider middlewares:
[‘scrapy.spidermiddlewares.httperror.HttpErrorMiddleware’,
‘scrapy.spidermiddlewares.offsite.OffsiteMiddleware’,
‘scrapy.spidermiddlewares.referer.RefererMiddleware’,
‘scrapy.spidermiddlewares.urllength.UrlLengthMiddleware’,
‘scrapy.spidermiddlewares.depth.DepthMiddleware’]
2019-02-24 18:46:24 [scrapy.middleware] INFO: Enabled item pipelines:
[‘weather.pipelines.WeatherPipeline’]
2019-02-24 18:46:24 [scrapy.core.engine] INFO: Spider opened
2019-02-24 18:46:24 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-02-24 18:46:24 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2019-02-24 18:46:25 [scrapy.core.engine] DEBUG: Crawled (200)
2019-02-24 18:46:25 [scrapy.core.engine] DEBUG: Crawled (200)
2019-02-24 18:46:25 [scrapy.core.engine] INFO: Closing spider (finished)
2019-02-24 18:46:25 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{‘downloader/request_bytes’: 345,
‘downloader/request_count’: 2,
‘downloader/request_method_count/GET’: 2,
‘downloader/response_bytes’: 63527,
‘downloader/response_count’: 2,
‘downloader/response_status_count/200’: 2,
‘finish_reason’: ‘finished’,
‘finish_time’: datetime.datetime(2019, 2, 24, 10, 46, 25, 421382),
‘log_count/DEBUG’: 2,
‘log_count/INFO’: 9,
‘response_received_count’: 2,
‘scheduler/dequeued’: 2,
‘scheduler/dequeued/memory’: 2,
‘scheduler/enqueued’: 2,
‘scheduler/enqueued/memory’: 2,
‘start_time’: datetime.datetime(2019, 2, 24, 10, 46, 24, 670356)}
2019-02-24 18:46:25 [scrapy.core.engine] INFO: Spider closed (finished)
wuhanspider:
import scrapy
from weather.items import WeatherItem
class WuhanspiderSpider(scrapy.Spider):
name = ‘wuhanSpider’
allowed_domains = [‘www.tianqi.com’]
start_urls = []
citys=[‘wuhan’,‘shanghai’]
for city in citys:
start_urls.append(‘http://www.tianqi.com/’+city+’/’)
def parse(self, response):
subSelector=response.xpath(’//div[@class=“tqshow1”]’)
items=[]
for sub in subSelector:
item=WeatherItem()
cityDates=’’
for cityDate in sub.xpath(’./h3//text()’).extract():
cityDates+=cityDate
item[‘cityDate’]=cityDates
item[‘week’]=sub.xpath(’./p//text()’).extract()[0]
item[‘img’]=sub.xpath(’./ul/li[1]/img/@src’).extract()[0]
temps=’’
for temp in sub.xpath(’/ul/li[2]//text()’).extract():
temps+=temp
item[‘temperature’]=temps
item[‘weather’] = sub.xpath(’./ul/li[3]//text()’).extract()[0]
item[‘wind’] = sub.xpath(’./ul/li[4]//text()’).extract()[0]
items.append(item)
yield items
pipelines:
import time
import os.path
from urllib import request
class WeatherPipeline(object):
def process_item(self, item, spider):
today=time.strftime(’%Y%m%d’,time.localtime())
fileName=today+’.txt’
with open(fileName,‘a’) as fp:
fp.write((item[‘cityDate’]+’\t’))
fp.write(item[‘week’]+’\t’)
imgName=os.path.basename(item[‘img’])
fp.write(imgName+’\t’)
if os.path.exists(imgName):
pass
else:
with open (imgName,‘wb’) as fp:
response=request.urlopen(item[‘img’])
fp.write(response.read())
fp.write(item[‘temperature’]+’\t’)
fp.write(item[‘weather’+’\t’])
fp.write(item[‘wind’] + ‘\t\n’)
time.sleep(1)
return item
settings:
OT_NAME = ‘weather’
SPIDER_MODULES = [‘weather.spiders’]
NEWSPIDER_MODULE = ‘weather.spiders’
DOWNLOADER_MIDDLEWARES = {
‘scrapy.downloadermiddlewares.useragent.UserAgentMiddleware’: None,
}
ROBOTSTXT_OBEY = False
ITEM_PIPELINES={‘weather.pipelines.WeatherPipeline’:300,}
这是执行结果,请各位大神赐教~不胜感激