我是scrapy的新手,只是在收集我的抓取数据时遇到了一些问题。 到目前为止,我有..
import scrapy
class userScrape(scrapy.Spider):
name = 'uscraper'
allowed_domains = ['twitter.com']
def start_requests(self):
url = 'https://www.twitter.com/'
tag = getattr(self, 'username', None)
if tag is not None:
url = url + tag
yield scrapy.Request(url, self.parse)
def parse(self, response):
self.logger.debug('callback "parse": got response %r' % response)
def parse_item(self, response):
item = scrapy.Item()
item['name'] = response.xpath('.//@data-name').extract()[0]
item['tweet_count'] = response.css('.ProfileNav-value::text').extract()[0]
return item
和...
import scrapy
class UserscrapeItem(scrapy.Item):
name = scrapy.Field()
tweet_count = scrapy.Field()
# define the fields for your item here like:
# name = scrapy.Field()
pass
当我刮胡子时使用..“scrapy crawl uscraper -a username = Twitter”我没有返回任何我要求的抓取数据..
请查看我的结果。
(base) C:\Users\NevilleBartos\userscrape>scrapy crawl uscraper -a
username=Twitter
2018-02-27 10:53:44 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot:
userscrape)
2018-02-27 10:53:44 [scrapy.utils.log] INFO: Versions: lxml 4.1.0.0, libxml2
2.9.5, cssselect 1.0.1, parsel 1.2.0, w3lib 1.18.0, Twisted 17.5.0, Python
3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC
v.1900 64 bit (AMD64)], pyOpenSSL 17.2.0 (OpenSSL 1.0.2n 7 Dec 2017),
cryptography 2.0.3, Platform Windows-10-10.0.16299-SP0
2018-02-27 10:53:44 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME':
'userscrape', 'NEWSPIDER_MODULE': 'userscrape.spiders', 'SPIDER_MODULES':
['userscrape.spiders']}
2018-02-27 10:53:44 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-02-27 10:53:44 [scrapy.middleware] INFO: Enabled downloader
middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'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']
2018-02-27 10:53:44 [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']
2018-02-27 10:53:44 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-02-27 10:53:44 [scrapy.core.engine] INFO: Spider opened
2018-02-27 10:53:44 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0
pages/min), scraped 0 items (at 0 items/min)
2018-02-27 10:53:44 [scrapy.extensions.telnet] DEBUG: Telnet console
listening on 127.0.0.1:6024
2018-02-27 10:53:44 [scrapy.downloadermiddlewares.redirect] DEBUG:
Redirecting (301) to <GET https://twitter.com/Twitter> from <GET
https://www.twitter.com/Twitter>
2018-02-27 10:53:45 [scrapy.core.engine] DEBUG: Crawled (200) <GET
https://twitter.com/Twitter> (referer: None)
2018-02-27 10:53:45 [uscraper] DEBUG: callback "parse": got response <200
https://twitter.com/Twitter>
2018-02-27 10:53:45 [scrapy.core.engine] INFO: Closing spider (finished)
2018-02-27 10:53:45 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 530,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 52800,
'downloader/response_count': 2,
'downloader/response_status_count/200': 1,
'downloader/response_status_count/301': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 2, 27, 9, 53, 45, 766083),
'log_count/DEBUG': 4,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'start_time': datetime.datetime(2018, 2, 27, 9, 53, 44, 811702)}
2018-02-27 10:53:45 [scrapy.core.engine] INFO: Spider closed (finished)
when using the scrapy shell i can extract the necessary data e.g.
scrapy shell https:/twitter.com/Twitter
response.xpath('.//@data-name').extract()[0]
我的结果是'推特'正如预期的那样..所以我认为我的回调我的蜘蛛或我的parse_item有问题。任何帮助都会非常感谢你
答案 0 :(得分:1)
您的代码中没有任何内容正在调用parse_item()
您必须自己调用它或将其作为请求的回调。
如何做到这一点的一个例子:
def parse(self, response):
self.logger.debug('callback "parse": got response %r' % response)
return self.parse_item(response)
另一个选择是让您的项目直接在parse()
中解析代码。