MCPcopy Create free account
hub / github.com/geekcomputers/Python / data

Function data

Wikipdedia/practice_beautifulsoap.py:25–51  ·  view source on GitHub ↗
(selected_topic, selected_language)

Source from the content-addressed store, hash-verified

23
24
25def data(selected_topic, selected_language):
26 symbol = language_symbols.get(selected_language)
27
28 try:
29 url = f"https://{symbol}.wikipedia.org/wiki/{selected_topic}"
30 data_response = requests.get(url)
31 data_response.raise_for_status()
32 data_soup = BeautifulSoup(data_response.content, "html.parser")
33
34 main_content = data_soup.find("div", {"id": "mw-content-text"})
35 filtered_content = ""
36
37 if main_content:
38 for element in main_content.descendants:
39 if element.name in ["h1", "h2", "h3", "h4", "h5", "h6"]:
40 filtered_content += (
41 "\n" + element.get_text(strip=True).upper() + "\n"
42 )
43
44 elif element.name == "p":
45 filtered_content += element.get_text(strip=True) + "\n"
46
47 return filtered_content
48
49 except requests.exceptions.RequestException as e:
50 print("Error fetching Wikipedia content:", e)
51 return "Error fetching data."
52
53
54def get_image_urls(query):

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected