Where can I find some “hello world”-simple Beautiful Soup examples?

后端 未结 2 1247
半阙折子戏
半阙折子戏 2021-02-04 16:55

I\'d like to do a very simple replacement using Beautiful Soup. Let\'s say I want to visit all A tags in a page and append \"?foo\" to their href. Can someone post or link to an

2条回答
  •  长发绾君心
    2021-02-04 17:41

    from BeautifulSoup import BeautifulSoup
    
    soup = BeautifulSoup('''
    
       Testing
       
         foo
         Bar
       
    ''')
    
    for link in soup.findAll('a'): # find all links
        link['href'] = link['href'] + '?foo'
    
    print soup
    

    That prints:

    
    Testing
    
    foo
    Bar
    
    
    

    The documentation also has some examples for changing attributes. It is an extensive tutorial that covers all common aspects of BeautifulSoup. I don't know what is missing from the documentation, maybe you should clarify.

提交回复
热议问题