-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from Lukas0907/newspiders
Add spider for python-patterns.guide and dietiwag.org
- Loading branch information
Showing
7 changed files
with
150 additions
and
14 deletions.
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
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,16 @@ | ||
.. _spider_dietiwag.org: | ||
|
||
dietiwag.org | ||
------------ | ||
Latest articles of `dietiwag.org <http://www.dietiwag.org>`_. | ||
|
||
Configuration | ||
~~~~~~~~~~~~~ | ||
Add ``dietiwag.org`` to the list of spiders: | ||
|
||
.. code-block:: ini | ||
# List of spiders to run by default, one per line. | ||
spiders = | ||
dietiwag.org | ||
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,21 @@ | ||
.. _spider_python-patterns.guide: | ||
|
||
python-patterns.guide | ||
--------------------- | ||
The latest articles from python-patterns.guide_. Since articles on | ||
python-patterns.guide_ do not have a publication date, the ``Last-Modified`` | ||
header is used for the updated field which might not be accurate or stable. | ||
I.e. old articles might have a newer value in the updated field even if they | ||
were not updated. | ||
|
||
Configuration | ||
~~~~~~~~~~~~~ | ||
Add ``python-patterns.guide`` to the list of spiders: | ||
|
||
.. code-block:: ini | ||
# List of spiders to run by default, one per line. | ||
spiders = | ||
python-patterns.guide | ||
.. _python-patterns.guide: http://python-patterns.guide |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import re | ||
|
||
import scrapy | ||
|
||
from feeds.loaders import FeedEntryItemLoader | ||
from feeds.spiders import FeedsXMLFeedSpider | ||
|
||
|
||
class DieTiwagOrgSpider(FeedsXMLFeedSpider): | ||
name = "dietiwag.org" | ||
allowed_domains = [name] | ||
start_urls = ["http://www.dietiwag.org/rss.xml.php"] | ||
|
||
_title = "dietiwag.org" | ||
_subtitle = "die andere seite der tiroler wasser kraft" | ||
_link = "http://www.{}".format(name) | ||
_icon = "http://www.{}/favicon.ico".format(name) | ||
|
||
def parse_node(self, response, node): | ||
il = FeedEntryItemLoader(selector=node) | ||
url = node.xpath("link/text()").extract_first() | ||
il.add_value("link", url) | ||
il.add_xpath("updated", "pubDate/text()") | ||
il.add_xpath( | ||
"title", | ||
"title/text()", | ||
# Use re.DOTALL since some titles have newlines in them. | ||
re=re.compile("(?:Artikel|Tagebuch): (.*)", re.DOTALL), | ||
) | ||
yield scrapy.Request(url, self._parse_article, meta={"il": il}) | ||
|
||
def _parse_article(self, response): | ||
remove_elems = [ | ||
".noprint", | ||
"form", | ||
".lineall > font[size='2'] > b:first-child", | ||
"font[size='2'] > br:first-child", | ||
"font[size='2'] > br:first-child", | ||
"font[size='2'] > br:last-child", | ||
"font[size='2'] > br:last-child", | ||
"font[size='2'] > br:last-child", | ||
] | ||
replace_regex = { | ||
r"\[\d{2}\.\d{2}\.\d{4}\]": "", | ||
# A0 is a non-breaking space in latin1. | ||
"\xA0": "", | ||
r"<br>\s*<br>\s*\d{1,2}\.\d{1,2}\.\d{4}\s*<br>": "", | ||
} | ||
il = FeedEntryItemLoader( | ||
response=response, | ||
base_url=response.url, | ||
remove_elems=remove_elems, | ||
replace_regex=replace_regex, | ||
parent=response.meta["il"], | ||
) | ||
il.add_css("author_name", ".sidebar .authors__name::text") | ||
if response.css(".printwidth2"): | ||
il.add_css("content_html", ".printwidth2 > font[size='2']") | ||
il.add_css("content_html", ".printwidth2 > font[size='3'] > font[size='2']") | ||
else: | ||
# Tagebuch | ||
il.add_css("content_html", ".lineall") | ||
il.add_value("category", "Tagebuch") | ||
yield il.load_item() |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import scrapy | ||
|
||
from feeds.loaders import FeedEntryItemLoader | ||
from feeds.spiders import FeedsSpider | ||
|
||
|
||
class PythonPatternsGuide(FeedsSpider): | ||
name = "python-patterns.guide" | ||
allowed_domains = [name] | ||
start_urls = ["http://{}".format(name)] | ||
|
||
_title = "Python Patterns" | ||
_link = "http://{}".format(name) | ||
|
||
def parse(self, response): | ||
for path in response.css(".toctree-l1 > a::attr(href)").extract(): | ||
yield scrapy.Request(response.urljoin(path), self._parse_article) | ||
|
||
def _parse_article(self, response): | ||
remove_elems = ["h1", "#contents", ".headerlink"] | ||
change_tags = {".admonition-title": "h2"} | ||
il = FeedEntryItemLoader( | ||
response=response, | ||
base_url=response.url, | ||
remove_elems=remove_elems, | ||
change_tags=change_tags, | ||
) | ||
il.add_value("link", response.url) | ||
il.add_value("author_name", "Brandon Rhodes") | ||
# Use "Last-Modified" field or fall back to "Date". | ||
updated = ( | ||
response.headers.get("Last-Modified", response.headers.get("Date")) | ||
).decode("ascii") | ||
il.add_value("updated", updated) | ||
il.add_css("title", "title::text") | ||
il.add_css("content_html", "body > .section") | ||
yield il.load_item() |