Categories
Python Code

Extracting JSON from JavaScript in a web page

Why would you want to do that?

Well, if you are web scraping using Python, and Scrapy for instance, you may need to extract reviews, or comments that are loaded from JavaScript. This would mean you could not use your css or xpath selectors like you can with regular html.

Parse

Instead, in your browser, check if you may be able to parse the code, beginning with ctrl + f, and “json” and track down some JSON in the form of a python dictionary. You ‘just’ need to isolate it.

web-scraping javascript pages
view-source to find occurrences of “JSON” in your page

The response is not nice, but you can gradually shrink it down, in Scrapy shell or python shell…

scrapy-shell-response
Figure 1 – The response

Split, strip, replace

From within Scrapy, or your own Python code you can split, strip, and replace, with the built-in python commands until you have just a dictionary that you can use with json.loads.

x = response.text.split('JSON.parse')[3].replace("\u0022","\"").replace("\u2019m","'").lstrip("(").split(" ")[0].strip().replace("\"","",1).replace("\");","")

Master replace, strip , and split and you won’t need regular expressions!

With the response.text now ready as a JSON friendly dictionary you can do this:

import json
q = json.loads(x)

comment = (q[‘doctor’][‘sample_rating_comment’])

comment.replace(“\u2019″,”‘”)
print(comment)

The key thing to remember to use when parsing the response text is to use the index, to pick out the section you want, and then make use of “\” backslash to escaped characters when you are working with quotes, and actual backslashes in the text you’re parsing.

parsed-response
Figure 2 – The parsed response

Conclusion

Rendering to HTML using Splash, or Selenium, or using regular expressions are not always essential. Hope this helps illustrate how you can extract values FROM a python dictionary FROM json FROM javascript !

You may see a mass of text on your screen to begin with, but persevere and you can arrive at the dictionary contained within…

Demo of getting a Python Dictionary from JSON from JavaScript

Categories
Python Code Scrapy

Scraping a JSON response with Scrapy

reference:
► https://stackoverflow.com/questions/44939247/scrapy-extract-ldjson#48131898

We can’t get the html we need using a normal selector so having located the ‘script’ section in the browser (Chrome/Developer Tools) we can load into a JSON object to manipulate.

 json.loads(response.xpath('//script[@type="application/ld+json"]//text()') to get the data from a page containing javascript 

Using json.loads

We extracted the output which was not available from just using a normal css or xpath selector in Scrapy.

See the JSON response in scrapy video

Categories
Python Code Scrapy

Web Scraping Introduction

As an overview of how web scraping works, here is a brief introduction to the process, with the emphasis on using Scrapy to scrape a listing site.

If you would like to know more, or would like us to scrape data from a specific site please get in touch.

*This article also assumes you have some knowledge of Python, and have Scrapy installed. It is recommended to use a virtual environment. Although the focus is on using Scrapy, similar logic will apply if you are using Beautiful Soup, or Requests. (Beautiful Soup does some of the hard work for you with find, and select).

Below is a basic representation of the process used to scrape a page / site

web scraping with scrapy
Web Scraping Process Simplified
Most sites you will want to scrape provide data in a list – so check the number per page, and the total number. Is there a “next page” link? – If so, that will be useful later on…
Identify the div and class name to use in your “selector”

Identifying the div and class name

Using your web browser developer tools, traverse up through the elements (Chrome = Inspect Elements) until you find a ‘div’ (well, it’s usually a div) that contains the entire advert, and go no higher up the DOM.

(advert = typically: the thumbnail + mini description that leads to the detail page)

The code inside the ‘div’ will be the iterable that you use with the for loop.
The “.” before the “//” in the xpath means you select all of them
eg. All 20, on a listings page that has 20 adverts per page

Now you have the xpath and checked it in scrapy shell, you can proceed to use it with a for loop and selectors for each piece of information you want to pick out. If you are using XPATH, you can use the class name from the listing, and just add “.” to the start as highlighted below.

This “.” ensures you will be able to iterate through all 20 adverts at the same node level. (i.e All 20 on the page).

parse

To go to the details page we use “Yield” but we also have to pass the variables that we have picked out on the main page. So we use ‘meta’ (or newer version = cb_kwargs”).

yield Request(absolute_url, callback=self.fetch_detail, meta={'link': link, 'logo_url': logo_url, 'lcompanyname':lcompanyname})

Using ‘meta’ allows us to pass variables to the next function – in this case it’s called “fetch_details” – where they will be added to the rest of the variables collected and sent to the FEEDS export which makes the output file.

There is also a newer, recommended version of “meta” to pass variables between functions in Scrapy: “cb_kwargs

Once you have all the data it is time to use “Yield” to send it to the FEEDS export.

The “FEEDS” method that let you write output to your chosen file format/destination

This is the format and destination that you have set for your output file.

*Note it can also be a database, rather than JSON or CSV file.

Putting it all together

See the fully working spider code here :

https://github.com/RGGH/Scrapy5/blob/master/yelpspider.py

You may wish to run all of your code from within the script, in which case you can do this:

# main driver

if __name__ == "__main__":

    process = CrawlerProcess()

    process.crawl(YelpSpider)

    process.start()

# Also you will need to add this at the start :

from scrapy.crawler import CrawlerProcess

Web Scraping – Summary

We have looked at the steps involved and some of the code you’ll find useful when using Scrapy.

Identifying the html to iterate through is the key

Try and find the block of code that has all of the listings / adverts, and then narrow it down to one advert/listing. Once you have done that you can test your code in “scrapy shell” and start building your spider.

(Scrapy shell can be run from your CLI, independent of your spider code):

xpath-starts-with
Scrapy shell is your friend!

! Some of this content may be easier to relate to if you have studied and completed the following : https://docs.scrapy.org/en/latest/intro/tutorial.html

If you have any questions please get in touch and we’ll be pleased to help.

Categories
Python Code Scrapy

Scrapy tips

Passing variables between functions using meta and cb_kwargs

This will cover how to use callback with “meta” and the newer “cb_kwargs”

The highlighted sections show how “logo_url” goes from parse to fetch_detail, where “yield” then sends it to the FEED export (output CSV file).

When using ‘meta’ you need to use ‘meta.get’ on the ‘response’ in the next function.

The newer, Scrapy recommended way is to use “cb_kwargs”

As you can see, there is no need to use any sort of “get” method in ‘fetch-detail’ so it’s simpler to use now, albeit with a slightly longer, less memorable name!

Watch the YouTube video on cb_kwargs
Categories
Python Code Scrapy

Scrapy : Yield

Yes, you can use “Yield” more than once inside a method – we look at how this was useful when scraping a real estate / property section of Craigslist.

Put simply, “yield” lets you run another function with Scrapy and then resume from where you “yielded”.

To demonstrate this it is best show it with a working example, and then you’ll see the reason for using it.

Source code for this Scrapy Project

https://github.com/RGGH/Scrapy4/blob/master/realestate_loader_with_geo.py

The difference with this project was that most of the usual ‘details‘ were actually on the ‘thumbnails’ / ‘listing’ page, with the exception of the geo data. (Longitude and Latitude).

So you could say this was a back-to-front website. Typically all the details would be extracted from the details page, accessed via a listings page.

Because we want to pass data (“Lon” and “Lat”) between 2 functions (methods) – we had to initialise these variables:

def __init__(self):
    self.lat =""
    self.lon = ""
Scrapy-Yield-Explanation
The “lat’ and ‘lon’ variables that are used in ‘parse_detail’ – their values then get passed to items back inside the parse method.

Next, the typical ‘parse’ code that identifies all of the ads (adverts) on the page – class name = “result-info”.

You could use either:

all_ads = response.xpath('//p[@class="result-info"]')

or

all_ads = response.css("p.result-info")

( XPATH or CSS – both get the same result, to use as the Selector )

start_requests

We coded this, but it would run even if we hadn’t, it’s the default scrapy method that gets the first URL and passes the output “response” to the next method : ‘parse’.

parse

This is the method that finds all of the adverts on page 1, and goes off to the details page and extracts the geo data.

Next it fills the scrapy fields in items.py with the data from the thumbnail listing for the property on the listings page and the geo data.

So the reason we described this as a back-to-front website is that the majority of the details come from the thumbnails/listing, and only 2 bits of data (“lon” and “lat”) come from the ‘details’ page.

Craigslist Scrapy Project - Listings page
Above : Throughout this article we’re referring to this as the “thumbnails/listings’ page
Scrapy-Details-Page :
Craigslist Scrapy Project
Above : This is what we typically refer to as the “details” page
Categories
Python Code

Nested Dictionaries

Summary : Read a JSON file, create a Dict, loop through and get keys and values from the inner Dict using Python.

uses : f.read / json.dumps / json.loads / list comprehension / for loop
Start with a JSON file – which looks like this in Notepad….

This post assumes you already have a nested dictionary saved as a JSON file. If not, you can download my json example.

We want to do 3 things:

  1. Import the JSON into a Dict type object

2. Display the Dict to check the format

3. Loop through the Dict to get the values that we need to print out.

Let’s get started,

Import the JSON file

First we need to use “import json” to work with the json format

Next, open the file “ytdict.json” and load it into an object

with open("ytdict.json") as f:
d = json.loads(f.read())

print(type(d)) # will give : <class 'dict'>

Check the contents, sort by int, otherwise 11 will appear before 2

Print the contents

print (json.dumps({int(x):d[x] for x in d.keys()}, indent=2, sort_keys=True))

The output should look like this :

Here you can see the outer dict, and then the inner dict with 2 key:value pairs

For loop with Nested Dictionaries

Write a new file, which will be the one we want to use as our actual program; ‘demo_nested_dict.py’

import json
# The 'real' code used in ytapithumbget.py
with open("ytdict.json") as f:
    data = json.loads(f.read())
    # nested dictionary needs 2 for loops
    for k,v in data.items():
        for v2 in v:
        	print (v2[1])
        	print("------------")

We loop through the outer dictionary, finding the key:value pairs, and then using the second for loop we iterate through the key:value pairs from the returned values of the outer dictionary.

We’ve picked out the “Title” and “Description” values from the JSON file (It’s from my ‘YouTube-Channel-Downloader’ : ytapithumbget.py Script)

Note the index of [1] – That picks out the values from the values in the first for loop.

If we had used [0] we would have ended up with this:

Iterating through Nested Dictionaries | JSON – watch the YouTube Video

Categories
Python Code

Automating a potentially repetitive task

Write 102 workplans or write some code to do it?

The was a requirement to write 102 workplans and word/docx files. The only data that was unique to each workplan was held in a csv – 1 row per workplan, so this is how we automated the creation of 102 word documents.

Categories
Python Code

Using an API

How to use a YouTube API to get images and video descriptions from any channel using Python.

This shows how you can use some code to get the thumbnails and descriptions for every video on a chosen YouTube channel. The output from the descriptions can be saved as a local copy for editing and re-uploading.

https://github.com/RGGH

We have provided a guide on how to get the API key and clone our code.

Feel free clone the redandgreen YouTube Description Updater from GitHub : https://github.com/RGGH/rng

Once you have set this up you can back up your metatdata and add additional text to the descriptions on ALL of your videos.

The downloader allows you to download Titles, Descriptions and Video ID to a JSON file. You can do this for ANY channel.

Also you can choose to download all of the high resolution thumbnail images.