Can you provide examples of parsing HTML?

后端 未结 29 2675
走了就别回头了
走了就别回头了 2020-11-22 13:49

How do you parse HTML with a variety of languages and parsing libraries?


When answering:

Individual comments will be linked to in answers to questions

29条回答
  •  不知归路
    2020-11-22 14:33

    language: Python
    library: BeautifulSoup

    from BeautifulSoup import BeautifulSoup
    
    html = ""
    for link in ("foo", "bar", "baz"):
        html += '%s' % (link, link)
    html += ""
    
    soup = BeautifulSoup(html)
    links = soup.findAll('a', href=True) # find  with a defined href attribute
    print links  
    

    output:

    [foo,
     bar,
     baz]
    

    also possible:

    for link in links:
        print link['href']
    

    output:

    http://foo.com
    http://bar.com
    http://baz.com
    

提交回复
热议问题