Last active
February 27, 2019 11:23
-
-
Save chrisjsimpson/189f4f2df8df122d26001080e06a9ca4 to your computer and use it in GitHub Desktop.
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
import urllib2 | |
from bs4 import BeautifulSoup | |
import json | |
import re | |
#fp = open('soup.txt') | |
#doc = fp.read() | |
response = urllib2.urlopen('https://www.bnpparibas.dz/trouver-une-agence/') | |
doc = response.read() | |
soup = BeautifulSoup(doc, 'html.parser') | |
branches = [] | |
for elm in soup.find_all("li", class_="agency-orange"): | |
#import pdb;pdb.set_trace() | |
cityAndPostcode = elm.find_all('a', class_='button3')[0].parent.next_sibling.next_sibling.next_element.next_element.next_element.replace('\n\t\t\t\t\t\t', '') | |
cityAndPostcode = re.split("([0-9]+)", cityAndPostcode) | |
city = cityAndPostcode[2].strip() | |
postcode = cityAndPostcode[1].strip() | |
branch = { | |
'name': elm.find_all('a', class_='button3')[0].text, | |
'address': { | |
'line_1': elm.find_all('a', class_='button3')[0].parent.next_sibling.next_sibling.next_element.strip(), | |
'city': city, | |
'postcode': postcode | |
} | |
} | |
branches.append(branch) | |
print json.dumps(branches) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs;
https://jsoneditoronline.org/?id=43492f1f30a34788a347bcedcd934275