Created
June 12, 2019 07:44
-
-
Save arildm/e5a4db15f8cf05ccec43f81101c493eb to your computer and use it in GitHub Desktop.
Scrapes svenskaplatser.se and outputs a headerless csv with {city},{streetname}
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 requests | |
from lxml.cssselect import CSSSelector | |
from lxml.html import fromstring | |
SITE = 'https://www.svenskaplatser.se/' | |
def links(url, selector): | |
html = requests.get(SITE + url).content.decode() | |
tree = fromstring(html) | |
sel = CSSSelector(selector) | |
return ((el.text, el.attrib['href']) for el in sel(tree)) | |
for city, url in links('', '#main p:not(#intro) a'): | |
for street, url in links(url, '#main li a'): | |
print('%s,%s' % (city, street)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment