How do you import beautifulsoup?
from bs4 import BeautifulSoup
Full sample script of using BeautifulSoup
What does the BeautifulSoup method find_all(‘a’) do?
It returns a result set of all hyperlinks on the page. Sample code as to how to extract all hyperlinks:
Find all ‘a’ tags (which define hyperlinks): a_tags
a_tags=soup.find_all(‘a’)
Print the URLs to the shell
for link in a_tags:
print(link.get(‘href’))
How do you load a json file?
import json with open(“a_movie.json”) as json_file: json_data = json.load(json_file)
Code to load and then read and display json as a dictionary?
Import package
import requests
Assign URL to variable: url
url = ‘http://www.omdbapi.com/?apikey=ff21610b&t=social+network’
Package the request, send the request and catch the response: r
r=requests.get(url)
Decode the JSON data into a dictionary: json_data
json_data=r.json()
Print each key-value pair in json_data
for k in json_data.keys():
print(k + ‘: ‘, json_data[k])