python requests beautifulsoup code example

Example 1: beautifulsoup and requesrs

from bs4 import BeautifulSoup
import requests

page = requests.get('https://www.timeanddate.com/worldclock/vietnam/hanoi')
soup = BeautifulSoup(page.text, 'html.parser')

soup.find_all(id='qlook')[0].find_all(id='ct')[0]

Example 2: python beautifulsoup get attibute

xmlData = None

with open('conf//test1.xml', 'r') as xmlFile:
    xmlData = xmlFile.read()

xmlDecoded = xmlData

xmlSoup = BeautifulSoup(xmlData, 'html.parser')

repElemList = xmlSoup.find_all('repeatingelement')

for repElem in repElemList:
    print("Processing repElem...")
    repElemID = repElem.get('id')
    repElemName = repElem.get('name')

    print("Attribute id = %s" % repElemID)
    print("Attribute name = %s" % repElemName)