Using BeautifulSoup to extract the title of a link

后端 未结 2 1807
小蘑菇
小蘑菇 2020-12-18 01:18

I\'m trying to extract the title of a link using BeautifulSoup. The code that I\'m working with is as follows:

url = \"http://www.example.com\"
source_code =         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 01:56

    Well, it seems you have put two spaces between s-access-detail-page and a-text-normal, which in turn, is not able to find any matching link. Try with correct number of spaces, then printing number of links found. Also, you can print the tag itself - print link

    import requests
    from bs4 import BeautifulSoup
    
    url = "http://www.amazon.in/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=python"
    source_code = requests.get(url)
    plain_text = source_code.content
    soup = BeautifulSoup(plain_text, "lxml")
    links = soup.findAll('a', {'class': 'a-link-normal s-access-detail-page a-text-normal'})
    print len(links)
    for link in links:
        title = link.get('title')
        print title
    

提交回复
热议问题