Skip to content

Code enhancements wiki toc #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added error handling, combined the url and file name pairs, used a li…
…st comprehension to simplify the creation, removed the duplicate import of the requests module, adjust readibality
  • Loading branch information
SilentJMA committed Sep 22, 2023
commit 6104c801221296ceec113f080ccbe66fb89f3157
39 changes: 15 additions & 24 deletions wiki_toc.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
import csv
import requests
from bs4 import BeautifulSoup
import requests


def get_data(url):
response = requests.get(url)
response.raise_for_status() # Add error handling for request
soup = BeautifulSoup(response.text, 'lxml')
table_of_contents = soup.find("div", id="toc")
headings = table_of_contents.find_all("li")
data = []
for heading in headings:
heading_text = heading.find("span", class_="toctext").text
heading_number = heading.find("span", class_="tocnumber").text
data.append({
'heading_number': heading_number,
'heading_text': heading_text,
})
headings = soup.find("div", id="toc").find_all("li")

data = [{'heading_number': heading.find("span", class_="tocnumber").text,
'heading_text': heading.find("span", class_="toctext").text}
for heading in headings]

return data


def export_data(data, file_name):
with open(file_name, "w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=['heading_number', 'heading_text'])
writer.writeheader()
writer.writerows(data)


def main():
url_to_parse = "https://en.wikipedia.org/wiki/Python_(programming_language)"
file_name = "python_toc.csv"
data = get_data(url_to_parse)
export_data(data, file_name)

url_to_parse = "https://en.wikipedia.org/wiki/Web_scraping"
file_name = "web_scraping_toc.csv"
data = get_data(url_to_parse)
export_data(data, file_name)
urls = [
("https://en.wikipedia.org/wiki/Python_(programming_language)", "python_toc.csv"),
("https://en.wikipedia.org/wiki/Web_scraping", "web_scraping_toc.csv")
]

for url, file_name in urls:
data = get_data(url)
export_data(data, file_name)

print('Done')


if __name__ == '__main__':
main()