python - Using BeautifulSoup to extract Titles from Text Box - Stack Overflow

I am attempting to write a code using beautiful soup that prints the text for the links in the left han

I am attempting to write a code using beautiful soup that prints the text for the links in the left handed gray box on this webpage. In this case the code should return

** Enchantments Bouldering
Aasgard Sentinel 
Argonaut Peak 
Cashmere Mountain 
Colchuck Balanced Rock 
Colchuck Peak 
Crystal Lake Tower 
Dragontail Peak 
Flagpole, The 
Headlight Basin 
Ingalls Peak 
Jabberwocky Tower 
Mt Stuart 
Nightmare Needles 
Prusik Peak 
Rat Creek Spires 
Sherpa Peak 
Stuart Lake Basin 
Viviane Campsite 
Witches Tower 

I am trying to generalize the wonderful answers to this very similar question, but when inspecting the source for my new web page, I can not find a table being used, and can't decipher what the container is to reference in the following lines of code:

table = soup.find(lambda tag: tag.name=='???' and tag.has_attr('??') and tag['id']=="???") 
rows = table.findAll(lambda tag: tag.name=='a')

I am attempting to write a code using beautiful soup that prints the text for the links in the left handed gray box on this webpage. In this case the code should return

** Enchantments Bouldering
Aasgard Sentinel 
Argonaut Peak 
Cashmere Mountain 
Colchuck Balanced Rock 
Colchuck Peak 
Crystal Lake Tower 
Dragontail Peak 
Flagpole, The 
Headlight Basin 
Ingalls Peak 
Jabberwocky Tower 
Mt Stuart 
Nightmare Needles 
Prusik Peak 
Rat Creek Spires 
Sherpa Peak 
Stuart Lake Basin 
Viviane Campsite 
Witches Tower 

I am trying to generalize the wonderful answers to this very similar question, but when inspecting the source for my new web page, I can not find a table being used, and can't decipher what the container is to reference in the following lines of code:

table = soup.find(lambda tag: tag.name=='???' and tag.has_attr('??') and tag['id']=="???") 
rows = table.findAll(lambda tag: tag.name=='a')
Share Improve this question asked Nov 16, 2024 at 23:05 Prince MPrince M 4413 silver badges14 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

It's unclear what problem you're having but to get the required output is trivial:

import requests
from bs4 import BeautifulSoup as BS

url = "https://www.mountainproject/area/110928184/stuart-enchantments"

with requests.get(url) as response:
    response.raise_for_status()
    soup = BS(response.text, "html.parser")
    for a in soup.select("div.lef-nav-row a"):
        print(a.get_text(strip=True))

Output:

** Enchantments Bouldering
Aasgard Sentinel
Argonaut Peak
Cashmere Mountain
Colchuck Balanced Rock
Colchuck Peak
Crystal Lake Tower
Dragontail Peak
Flagpole, The
Headlight Basin
Ingalls Peak
Jabberwocky Tower
Mt Stuart
Nightmare Needles
Prusik Peak
Rat Creek Spires
Sherpa Peak
Stuart Lake Basin
Viviane Campsite
Witches Tower

if you already have the soup, then you just need to find the main container. here it's a div of class mp-sidebar, then take all links

links = soup.select('div.mp-sidebar a')
for link in links:
    print(link.text)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644912a4637910.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信