Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memeScraper1 #1

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions imagexam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 19 16:15:00 2019

@author: ADARSH ANURAG
"""
import pandas as pd
import requests
df = pd.read_csv("memedroid.csv")
for x in df['image']:
overlay=True
url=x
payload = {'url': url,
'isOverlayRequired':overlay,
'apikey': '9ec509657e88957',
'language': 'eng',
}
r = requests.post('https://api.ocr.space/parse/image',data=payload,)
print(r.status_code, r.reason)
result = r.json()
print(['ParsedResults'][0]['ParsedText'])
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions memeScraper/memeScraper/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class Meme(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
image = scrapy.Field()
tags = scrapy.Field()
103 changes: 103 additions & 0 deletions memeScraper/memeScraper/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals


class MemescraperSpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.

# Should return None or raise an exception.
return None

def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.

# Must return an iterable of Request, dict or Item objects.
for i in result:
yield i

def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.

# Should return either None or an iterable of Response, dict
# or Item objects.
pass

def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.

# Must return only requests (not items).
for r in start_requests:
yield r

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)


class MemescraperDownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.

# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None

def process_response(self, request, response, spider):
# Called with the response returned from the downloader.

# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response

def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.

# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
11 changes: 11 additions & 0 deletions memeScraper/memeScraper/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- 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 MemescraperPipeline(object):
def process_item(self, item, spider):
return item
25 changes: 25 additions & 0 deletions memeScraper/memeScraper/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

# Scrapy settings for memeScraper project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://doc.scrapy.org/en/latest/topics/settings.html
# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'memeScraper'

SPIDER_MODULES = ['memeScraper.spiders']
NEWSPIDER_MODULE = 'memeScraper.spiders'

#Export as csv feed
FEED_FORMAT = "csv"
FEED_URI = "memedroid.csv"

# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'memeScraper (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True
4 changes: 4 additions & 0 deletions memeScraper/memeScraper/spiders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
Binary file not shown.
Binary file not shown.
35 changes: 35 additions & 0 deletions memeScraper/memeScraper/spiders/memeSpider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 26 15:10:15 2018

@author: ADARSH ANURAG
"""
from scrapy import Spider
from scrapy.selector import Selector
from memeScraper.items import Meme

class MemeSpider(Spider):
name ="memebot"
allowed_domain = ["https://www.memedroid.com"]
start_urls = ["https://www.memedroid.com/memes/latest/"]

def parse(self,response):
Title=[]
Image=[]
Tags=[]
container = response.xpath("//div[@class='gallery-memes-container']")
articles = container.xpath(".//article")
for article in articles:
title = article.xpath(".//div[@class='item-aux-container']/header/h1/a/text()").extract()
Title.append(title[0])
image_link = article.xpath(".//div[@class='item-aux-container']/a[@class='dyn-link']/img/@src").extract()
Image.append(image_link[0])
tag = article.xpath(".//div[@class='tags-container']/a/text()").extract()
Tags.append(tag)
print('\n')
Item = Meme()
for item in zip(Title,Image, Tags):
Item['title'] = item[0]
Item['image'] = item[1]
Item['tags'] = item[2]
yield Item
11 changes: 11 additions & 0 deletions memeScraper/scrapy.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Automatically created by: scrapy startproject
#
# For more information about the [deploy] section see:
# https://scrapyd.readthedocs.io/en/latest/deploy.html

[settings]
default = memeScraper.settings

[deploy]
#url = http://localhost:6800/
project = memeScraper
26 changes: 26 additions & 0 deletions purpose of memeScraper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Purpose of memeScraper:

This Scraper scrapes the title, image and tags for a meme from https://www.memedroid.com.

## Tools used:

Scrapy

## How to run:

Run the memeSpider of memeScraper.
Type this in command line
```
scrapy crawl memebot
```

## Output:

This scraper returns title, image and tags for a meme in a csv file memedroid.csv.

imagexam.py makes use of ocr space api to find texts inside the meme images for classification.

## Future Works to be Performed:
* Scrap memes from more websites.
* Use of machine learning to provide relevant tags to meme without tags.
* Make a suitable GUI for the resulting application.