<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jack on Medium]]></title>
        <description><![CDATA[Stories by Jack on Medium]]></description>
        <link>https://medium.com/@progblackcw?source=rss-2a31a9033db7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0895NHILiRcYkaTr.</url>
            <title>Stories by Jack on Medium</title>
            <link>https://medium.com/@progblackcw?source=rss-2a31a9033db7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:59:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@progblackcw/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Elaborating on Scrapy Spiders with XPath:]]></title>
            <link>https://medium.com/@progblackcw/elaborating-on-scrapy-spiders-with-xpath-2d917e11c679?source=rss-2a31a9033db7------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d917e11c679</guid>
            <dc:creator><![CDATA[Jack]]></dc:creator>
            <pubDate>Tue, 09 May 2023 12:38:59 GMT</pubDate>
            <atom:updated>2023-05-09T12:38:59.986Z</atom:updated>
            <content:encoded><![CDATA[<p>XPath expressions are what scrapy module is fundamentally comprised of.</p><h4>Why is it useful:</h4><ul><li>navigate structure</li><li>observe content</li><li>specify with higher resolution what data you wish to extract/navigate to</li></ul><p>Experimentation of XPath in the shell to extract authors from example site:</p><pre>scrapy shell &#39;https://quotes.toscrape.com&#39;<br>response.css(&quot;div.quote&quot;)<br>text = quote.css(&quot;span.text::text&quot;).get()<br>text<br>author = quote.css(&quot;small.author::text&quot;).get()<br>author<br>tags = quote.css(&quot;div.tags a.tag::text&quot;).getall()<br>tags </pre><p>This process can also be automated by creating a python script containing the code for a spider with much more detailed information.</p><blockquote>(Tip: to exit the shell simply type exit.)</blockquote><h4>Json Lines (.jsonl):</h4><p>An introduction to json lines… why and how it is used to store data.</p><p>Having entered your folder containing the spider script, type:</p><pre>scrapy crawl quotes -o quotes.jsonl</pre><p>into the terminal.</p><p>This will extract all of the data from quotes into a json lines file in your folder named quotes.jsonl.</p><p>Json lines is a great tool as it maintains the dictionary structure of regular json files, however it adds line by line so you can append more data to the file as you go without messing up the organisation of the file.</p><h4>Link extraction in the shell:</h4><p>One can use XPath commands to extract links from webpages and follow them, this allows for more complex spiders and data extraction since you can mine for links on one page and then jump to other webpages from just one initial page.</p><p>This therefore has applications for automation.</p><p>For example:</p><pre>response.css(&#39;li.next a::attr(href)&#39;)</pre><p>li is the element, next is the class, a refers to the nested a element. attr() is the attribute selection method,</p><p>:: is the way one calls a method upon an element, then href is the attribute within the element you are selecting,</p><p>which gives you the link subpages which you can now append to links in python scripts.</p><h4>In conclusion:</h4><p><em>XPath allows you to take your spiders to the next level as it has much more specific and detailed data selection tools, especially regarding embedded links.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d917e11c679" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Scrapy to create spiders for beginners.]]></title>
            <link>https://medium.com/@progblackcw/how-to-use-scrapy-to-create-spiders-for-beginners-90b44cc55274?source=rss-2a31a9033db7------2</link>
            <guid isPermaLink="false">https://medium.com/p/90b44cc55274</guid>
            <category><![CDATA[web-scraping]]></category>
            <dc:creator><![CDATA[Jack]]></dc:creator>
            <pubDate>Sun, 07 May 2023 20:31:05 GMT</pubDate>
            <atom:updated>2023-05-07T20:31:05.228Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to try and learn how to use scrapy, as I was very interested in webscraping, given its use in data science.</p><p>I did some research and found scrapy, this is a python module that is great for webscraping. Using Scrapy, you construct objects called ‘Spiders’, these are objects in a python script that you then run from a python shell in the terminal. I ran it in python 3.10.8 on Ubuntu 20.04</p><p>An example Spider (I will show you how to build and run).</p><pre>class QuotesSpider(scrapy.Spider):<br>    name = &quot;quotes&quot;<br>    start_urls = [<br>        &#39;https://quotes.toscrape.com/page/1/&#39;,<br>        &#39;https://quotes.toscrape.com/page/1/&#39;<br>    ]<br><br>    def parse(self,response):<br>        for quote in response.css(&#39;div.quote&#39;):<br>            yield {<br>                &#39;text&#39;: quote.css(&#39;span.text::text&#39;).get(),<br>                &#39;author&#39;: quote.css(&#39;small.author::text&#39;).get(),<br>                &#39;tags&#39;: quote.css(&#39;div.tags a.tag::text&#39;).getall(),<br>            }</pre><p><strong>Step one:</strong> Installation of scrapy on terminal.</p><pre>pip install scrapy <br>scrapy startproject tutorial<br></pre><p><strong>Step two:</strong> Construction of Spider (scraping object).</p><pre>from pathlib import Path<br><br>import scrapy<br><br>class QuotesSpider(scrapy.Spider):<br>    name = &quot;quotes&quot;<br><br>    def start_requests(self):<br>        urls = [<br>            &#39;https://quotes.toscrape.com/page/1/&#39;,<br>            &#39;https://quotes.toscrape.com/page/2/&#39;<br>        ]<br>        for url in urls:<br>            yield scrapy.Request(url=url, callback=self.parse)<br><br>    def parse(self, response):<br>        page = response.url.split(&quot;/&quot;)[-2]<br>        filename = f&#39;quotes-{page}.html&#39;<br>        Path(filename).write_bytes(response.body)<br>        self.log(f&#39;Saved file {filename}&#39;)</pre><p><strong>Step three</strong>: running the spider</p><p>Simply navigate into folder containing the quotes_spider.py script.</p><p>then run:</p><pre>scrapy crawl quotes</pre><p>This initiates the shell.</p><p>Now, to actually execute the script and scrape data from a site, you now run ‘scrapy shell website_url’, for example:</p><pre>scrapy shell &#39;https://quotes.toscrape.com/page/1/&#39;</pre><p>This outputs the Spider’s data log into the terminal, as well as allowing for input of commands and output of results.</p><p>One can select elements of the html data. For example if you type:</p><pre>response.css(&#39;title&#39;)</pre><p>… it will return a response containing all of the ‘title’ element data, including all nested elements etc.</p><h4>SelectorLists:</h4><p>Responses are in fact returning an object known as a SelectorList, which are better understood as a list of selector objects wrapped around XML/HTML elements.</p><p>What is the utiltity of SelectorLists?</p><p>Well, the response provides fully intact HTML data (or XML) that can be navigated and observed.</p><p>This allows one to finetune requests in real time, so one can extract one’s required data faster and more efficiently.</p><p>Finetuning your results in the shell:</p><p>When extracting data into the shell, you can add as many levels of abstraction with regards to the html nesting as you require.</p><p>For example:</p><pre>response.css(&#39;title::text&#39;)[0].get()</pre><p>This is the same code as earlier, except, firstly we specify only the ::text part of the title element, then within that we are selecting the first item in the list. This is all a css query.</p><h4>Filtering requests using regex:</h4><p>If you are comfortable with regex, (regular expression), we can utilise the .re() method (obviously you will need to have regex python package installed).</p><p>Example usage:</p><pre>response.css(&#39;title::text&#39;).re(r&#39;Quotes.*&#39;)</pre><p>This is filtering to return strings from the list that is the text part of the ‘title’ element, that begin with the word ‘Quotes’.</p><p>Additional points:</p><p>If you are confident with using the shell to extract SelectorLists, you can also help inform which CSS selectors you use by opening the data in your browser. Simply type:</p><pre>view(response)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90b44cc55274" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>