只是一个关于 Scrapy 中 json 导出格式的快速问题。我导出的文件看起来像这样。
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
{"pages": {"title": "x", "text": "x", "tags": "x", "url": "x"}}
Run Code Online (Sandbox Code Playgroud)
但我希望它采用这种确切的格式。不知何故,我需要在“页面”下获取所有其他信息。
{"pages": [
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"},
{"title": "x", "text": "x", "tags": "x", "url": "x"}
]}
Run Code Online (Sandbox Code Playgroud)
我在scrapy或python方面不是很有经验,但除了导出格式之外,我已经在我的蜘蛛中完成了所有其他工作。这是我的pipelines.py,我刚开始工作。
from scrapy.exporters import JsonItemExporter
import json
class RautahakuPipeline(object):
def open_spider(self, spider):
self.file = open('items.json', 'w')
def close_spider(self, spider):
self.file.close() …Run Code Online (Sandbox Code Playgroud) 我需要使用以下命令访问从 CLI 传递的自定义设置:
-s SETTING_NAME="SETTING_VAL"
来自蜘蛛类的 __init__() 方法。
get_project_settings()允许我仅访问静态设置。
该文档解释了如何通过以下方式从管道设置新管道来访问这些自定义设置:
@classmethod
def from_crawler(cls, crawler):
settings = crawler.settings
Run Code Online (Sandbox Code Playgroud)
但是有什么方法可以通过__init__()蜘蛛方法访问它们吗?
之前曾有人问过这个问题,但总会出现的答案是使用DjangoItem。但是它在github上指出:
通常对于写密集型应用程序(例如Web爬网程序)来说不是一个好的选择...可能无法很好地扩展
这是我问题的症结所在,我想以与我运行python manage.py shell以及从myapp.models import Model1进行操作时相同的方式使用Django模型并与之交互。使用查询如此处所示。
我已经尝试了相对导入,并将我的整个scrapy项目移到了django应用程序中,但都无济于事。
我应该将我的拼凑项目移到哪里进行这项工作?我该如何重新创建/使用所有在Scrapy管道内的Shell中可用的方法?
提前致谢。
我有用 python 3.6 编写的 scrapy 项目。该项目有 3 个爬虫,它只需从 3 个不同的网站抓取项目,每个网站一个爬虫。items.py我正在使用脚本中的项目yield item,每个爬虫在项目中都有细微的不同,我运行它scrapy crawl crawlera -o sample.json并获取sample.json文件作为输出文件。我对每个爬虫执行相同的操作,但输出文件名不同。
但是,我想做的是,我想timestamp + website name为每个网站提供文件名,这样每次运行和每个网站的文件名都会不同。
所有三个爬虫都有相同的结构,如下所示
# -*- coding: utf-8 -*-
import scrapy
import logging
from time import sleep
from selenium import webdriver
from scrapy.selector import Selector
from scrapy.utils.log import configure_logging
from product_data_scraper.items import TakealotItem
from product_data_scraper.spiders.helper import Helper
class TakealotSpider(scrapy.Spider):
name = 'takealot'
allowed_domains = ['www.takealot.com']
takealothelper = Helper.TakealotHelper
driver_path = './chromedriver'
configure_logging(install_root_handler=False)
logging.basicConfig(
filename='logs/log_takealot.txt', …Run Code Online (Sandbox Code Playgroud) 我正在编写一个爬虫爬虫来抓取 youtube 视频并捕获、名称、订阅者计数、链接等。我从教程中复制了这个 SQLalchemy 代码并让它工作,但是每次我运行爬虫时,我都会在数据库中得到重复的信息。
我如何检查抓取的数据是否已经在数据库中,如果是,请不要进入数据库....
这是我的 pipeline.py 代码
from sqlalchemy.orm import sessionmaker
from models import Channels, db_connect, create_channel_table
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
class YtscraperPipeline(object):
"""YTscraper pipeline for storing scraped items in the database"""
def __init__(self):
#Initializes database connection and sessionmaker.
#Creates deals table.
engine = db_connect()
create_channel_table(engine)
self.Session = sessionmaker(bind=engine)
def process_item(self, item, spider):
"""Save youtube channel …Run Code Online (Sandbox Code Playgroud) 几天来我一直在试图弄清楚如何安排我的 scrapy 蜘蛛,但没有任何运气。(我尝试了从 Windows 任务计划程序到 scrapy-do lib 的所有内容,但在我的MAIN.PY上没有任何效果)
(我的主要目标是安排我的蜘蛛每 5 分钟从我的蜘蛛NewsSpider收集数据到 mySQL news_db数据库)
请查看我的脚本,因为它有所修改,并根据需要进行更改。我真的希望这个能发挥作用。
主程序.PY
from scrapy import cmdline
cmdline.execute("scrapy crawl news".split())
Run Code Online (Sandbox Code Playgroud)
新闻_蜘蛛.PY
import scrapy
from ..items import WebspiderItem
class NewsSpider(scrapy.Spider):
name = 'news'
start_urls = [
'https://www.coindesk.com/feed'
]
def parse(self, response):
pub_date = response.xpath('//pubDate/text()').extract()[0]
page_title = response.xpath('//title/text()').extract()[2]
page_summary = response.xpath('//description/text()').extract()[1]
text_link = response.xpath('//link/text()').extract()[2]
item = WebspiderItem()
item['date'] = pub_date
item['title'] = page_title
item['summary'] = page_summary
item['link'] = text_link
yield item
Run Code Online (Sandbox Code Playgroud)
项目.PY
import scrapy
class WebspiderItem(scrapy.Item): …Run Code Online (Sandbox Code Playgroud) 我的 scrapy 中有这个管道,我需要从 Scrapy 统计信息中获取信息
class MyPipeline(object):
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.stats)
def process_item(self, item, spider):
print self.stats.get_stats()['item_scraped_count']
return item
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到此错误
Traceback (most recent call last):
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\twisted\internet\defer.py", line 649, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\<project_name>\pipelines.py", line 35, in process_item
print self.stats.get_stats()['item_scraped_count']
KeyError: 'item_scraped_count'
Run Code Online (Sandbox Code Playgroud)
如果这不是获取统计值的正确方法,那么我该怎么办?
我正在使用scrapy刮掉一些大品牌来导入我网站的销售数据.目前我正在使用
DOWNLOAD_DELAY = 1.5
CONCURRENT_REQUESTS_PER_DOMAIN = 16
CONCURRENT_REQUESTS_PER_IP = 16
Run Code Online (Sandbox Code Playgroud)
我使用Item加载器指定css/xpath规则和Pipeline将数据写入csv.我收集的数据是原价,销售价格,颜色,尺寸,名称,图片网址和品牌.
我只为一个拥有大约10万网址的商家写了蜘蛛,这需要我大约4个小时.
我的问题是,对于10k网址,4小时听起来是否正常,或者它应该比这更快.如果是这样,我还需要做些什么来加快速度.
我只在本地使用一个SPLASH实例进行测试.但在生产中我计划使用3个SPLASH实例.
现在主要问题是,我有大约125个商家和每个平均10k产品.他们中的一对有超过150k的网址.
我需要每晚清理所有数据以更新我的网站.由于我的单个蜘蛛花了4个小时来刮掉10k网址,我想知道每晚实现125 x 10k网址是否真的是有效的梦想
我将非常感谢您对我的问题的经验输入.