Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SilentJMA wants to merge2 commits intooxylabs:main
base:main
Choose a base branch
Loading
fromSilentJMA:code-enhancements-wiki-toc
Open
Changes fromall commits
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
50 changes: 26 additions & 24 deletionswiki_toc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
import csv
import requests
from bs4 import BeautifulSoup
import requests


def get_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
table_of_contents =soup.find("div", id="toc")
headings = table_of_contents.find_all("li")
response.raise_for_status()
soup =BeautifulSoup(response.text, 'html.parser')

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,
})

toc = soup.find("div", {"id": "toc"})

if toc:
headings = toc.find_all("li")
for heading in headings:
heading_number = heading.find("span", {"class": "tocnumber"})
heading_text = heading.find("span", {"class": "toctext"})

if heading_number and heading_text:
data.append({
'heading_number': heading_number.text.strip(),
'heading_text': heading_text.text.strip(),
})

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()

[8]ページ先頭

©2009-2025 Movatter.jp