-
Notifications
You must be signed in to change notification settings - Fork 5
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
Pull request for crawler #5
Open
jitendravarma
wants to merge
3
commits into
fafadiatech:master
Choose a base branch
from
jitendravarma:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
39 changes: 39 additions & 0 deletions
39
scrapy_ft_jobs_sites/scrapy_ft_jobs_sites/crawler/services.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from hashlib import md5 | ||
from datetime import datetime | ||
|
||
from scrapy_ft_jobs_sites.settings import CRAWLER_DIR | ||
|
||
MAX_HASH_CHARS = 8 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to |
||
|
||
|
||
def check_or_create_directory(spider_name): | ||
""" | ||
this is used to create directory for storing the html content | ||
for a given spider on its domain name | ||
""" | ||
|
||
crawl_id = md5(str(datetime.now().strftime("%Y%m%d")) | ||
).hexdigest()[:MAX_HASH_CHARS] | ||
base_directory = os.path.join(CRAWLER_DIR[0], spider_name) | ||
|
||
all_directories = [base_directory, os.path.join(base_directory, crawl_id)] | ||
|
||
# this is used to check if the directory exists or not then its creates one | ||
for current_dir in all_directories: | ||
if not os.path.exists(current_dir): | ||
os.mkdir(current_dir) | ||
|
||
return crawl_id | ||
|
||
|
||
def cache_response(spider_name, html_response): | ||
""" | ||
this method is used to save HTML response to disk | ||
""" | ||
crawl_id = check_or_create_directory(spider_name) | ||
page_id = md5(str(datetime.now())).hexdigest()[:MAX_HASH_CHARS] | ||
file_path = os.path.join(CRAWLER_DIR[0], spider_name, crawl_id, page_id) | ||
with open(file_path, "w") as file_to_save: | ||
file_to_save.write(html_response.encode("utf-8")) | ||
file_to_save.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from .base import BaseFTSpider | ||
from .parsers import IndeedParser | ||
|
||
from scrapy_ft_jobs_sites.crawler.services import cache_response | ||
|
||
|
||
class IndeedInSpider(BaseFTSpider): | ||
name = 'indeedin' | ||
|
@@ -25,10 +27,8 @@ class IndeedInSpider(BaseFTSpider): | |
follow=False), | ||
) | ||
|
||
|
||
def __init__(self, *args, **kwargs): | ||
super(IndeedInSpider, self).__init__(*args, **kwargs) | ||
|
||
# Logic for start_urls creations | ||
self.start_urls = [] | ||
|
||
|
@@ -37,10 +37,13 @@ def __init__(self, *args, **kwargs): | |
self.start_urls.append(URL) | ||
|
||
for pagination in range(10, 60, 10): | ||
URL = self.base_url_pattern + item.replace(" ", "+") + "&start=" + str(pagination) | ||
URL = self.base_url_pattern + \ | ||
item.replace(" ", "+") + "&start=" + str(pagination) | ||
self.start_urls.append(URL) | ||
|
||
def parse_item(self, response): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure you have test case for parsing logic, this will help making sure our standard is enforced |
||
soup = BeautifulSoup(response.body, 'html.parser') | ||
parser = IndeedParser(soup, "".join(self.allowed_domains)) | ||
return parser.parse_response() | ||
# this function will save the html content to disk | ||
cache_response(self.name, soup.text) | ||
return parser.parse_response() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should possibly be called
utils.py