Posts

Showing posts from July, 2016

How to get href value of each a tag from the html using python Beautiful Soup

Image
import requests from bs4 import BeautifulSoup link = "http://www.flipkart.com/mobiles?otracker=hp_header_nmenu_sub_Electronics_0_Mobiles" doc = requests.get(link) soup = BeautifulSoup(doc.text, 'html.parser') main_div = soup.find(id="list-tagcloud") div2=main_div.find_all('div')[1] links = div2.find_all('a') for link in links:     print link.attrs.get('href') OUTPUT ====== /mobiles/motorola~brand/pr?sid=tyy,4io /mobiles/lenovo~brand/pr?sid=tyy,4io /mobiles/samsung~brand/pr?sid=tyy,4io /mobiles/leeco~brand/pr?sid=tyy,4io /yu-yunicorn/p/itmejeuf7egdedar?pid=MOBEJ3MF23Q9MGMH /mobiles/honor~brand/pr?sid=tyy,4io /mobiles/mi~brand/pr?sid=tyy,4io /mobiles/asus~brand/pr?sid=tyy,4io /mobiles/apple~brand/pr?sid=tyy,4io /mobiles/intex~brand/pr?sid=tyy,4io /mobiles/sony~brand/pr?sid=tyy,4io /mobiles/alcatel~brand/pr?sid=tyy,4io /mobiles/lava~brand/pr?sid=tyy,4io /gionee-store /mobiles/pr?sid=tyy,4io

How to dispaly data from html tags using python Beautiful Soup

Image
import requests from bs4 import BeautifulSoup link = "http://www.flipkart.com/mobiles?otracker=hp_header_nmenu_sub_Electronics_0_Mobiles" doc = requests.get(link) soup = BeautifulSoup(doc.text, 'html.parser') main_div = soup.find(id="list-tagcloud") div2=main_div.find_all('div')[1] for x in div2.strings:     print x OUTPUT ====== Motorola Lenovo Samsung LeEco Yunicorn Honor Mi Asus Apple Intex Sony Alcatel Lava Gionee All

Find all the URLs found within a page’s using python

Find all the URLs found within a page’s from bs4 import BeautifulSoup html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html_doc, 'html.parser') for link in soup.find_all('a'):     print link.get('href&

How to use BeautifulSoup in python

BeautifulSoup 4 in Python.Beautiful Soup is a Python library for pulling data out of HTML and XML files. html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') #prin