Last active
May 12, 2018 14:55
-
-
Save wf34/c55e265c44b3ca4b7102bee5693efbfe to your computer and use it in GitHub Desktop.
Web Scraping Wikipedia
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 | |
import lxml.html as html | |
def get_page(url): | |
return requests.get(url, timeout =1.).text | |
def parse_page(song_url): | |
page_source = get_page(song_url) | |
assert isinstance(page_source, str) | |
root = html.fromstring(page_source) | |
TARGET_XPATH = '//*[@id="mw-content-text"]/div/p[11]' | |
target_html = root.xpath(TARGET_XPATH) | |
assert len(target_html) == 1 | |
target_html = target_html[0] | |
output = [target_html.text] | |
for c in target_html.getchildren(): | |
output.extend([c.text, c.tail]) | |
return list(filter(lambda x : x, output)) | |
target_url = 'https://en.wikipedia.org/wiki/Periodic_table' | |
print(' '.join(parse_page(target_url))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment