Skip to content

Instantly share code, notes, and snippets.

@amferraz
amferraz / gist:5143268
Last active December 14, 2015 20:19 — forked from anonymous/gist:5142892
from scrapy.utils.project import get_project_settings
def stop_reactor():
reactor.stop() #Stops reactor to prevent script from hanging
if __name__ == '__main__':
#Handles engine_stopped to stop twisted reactor
dispatcher.connect(stop_reactor, signal=signals.engine_stopped)

Requirements

  • bootstrap with typeahead
  • jquery

Explanation

This will use bootstrap with typeahead to create an autocomplete search.

@amferraz
amferraz / spider_start_url.py
Last active December 17, 2015 15:09
A simple spider with a custom start url
class CustomSpider(BaseSpider):
def __init__(self, start_url=None, **kwargs):
super(BaseSpider, self).__init__(*args, **kwargs)
if start_url:
self.start_url = start_url
def start_requests(self):
return [Request(self.start_url)]
# -*- coding:utf-8 -*-
import pika
from pika import exceptions
from pika.adapters import twisted_connection
from twisted.internet import defer, reactor, protocol, task
# First of all, what is a Twisted deferred?
# reference http://migre.me/eBL9F
#
#!/usr/bin/env python
# http://www.rabbitmq.com/tutorials/tutorial-two-python.html
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
message = ' '.join(sys.argv[1:]) or "Hello World!"
@amferraz
amferraz / Vagrantfile
Created September 23, 2013 14:50
A sample Vagranfile for vagrant-aws. Fill in the "MY_*" values
Vagrant.configure("2") do |config|
config.vm.box = "MY_BOX_NAME"
config.vm.provider :aws do |aws, override|
aws.access_key_id = "MY_ACCESS_KEY_ID"
aws.secret_access_key = "MY_SECRET_ACCESS_KEY"
aws.keypair_name = "MY_KEYPAIR_NAME"
aws.security_groups = "MY_VAGRANT_SECURITY_GROUP"
# take a look here for AMIs/regions info:
@amferraz
amferraz / machine_name.py
Created September 25, 2013 13:34
Using python-vagrant to discover running VMs addresses https://pypi.python.org/pypi/python-vagrant
# -*- coding: utf-8 -*-
from vagrant import Vagrant
vm_name = 'vm_name_on_vagrantfile'
v = Vagrant()
hostname = v.hostname(vm_name=vm_name)
@amferraz
amferraz / main.py
Last active December 23, 2015 22:29
An example of ItemPipeline with FollowAllSpider
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import signals
from testspiders.spiders.followall import FollowAllSpider
class MyPipeline(object):
def process_item(self, item, spider):
Function Shortcut
previous tab ⌘ + left arrow
next tab ⌘ + right arrow
go to tab ⌘ + number
go to window ⌘ + Option + Number
go to split pane by direction ⌘ + Option + arrow
go to split pane by order of use ⌘ + ] , ⌘ + [
split window horizontally (same profile) ⌘ + D
split window vertically (same profile) ⌘ + d
@amferraz
amferraz / main.py
Last active December 25, 2015 18:59
A simple template scraper
# coding: utf-8
import requests
from lxml import html
home = requests.get('http://www.submarino.com.br/')
home_tree = html.fromstring(home.text)
products_links_xpath = '//*[@id="tab"]/div/ul/li/div/a/@href'
products_links = home_tree.xpath(products_links_xpath)