I was trying to scrape a website using python request and lxml. I could easily select the elements with single class using html.xpath() but I can't figure out how to select the elements with multiple class.
I used some code like this to select the elements in page with class "title":
page.xpath('//a[@class="title"]')
However, I couldn't select elements with multiple classes. I checked some few codes. I tried to study xpath but it seemes like lxml.html.xpath() works different, may be it's my lack of understanding. I tried few codes which didnt' work for me. They are given below.
HTML code
<a href="https://www.lovemycosmetic.de/skin1004-madagascar-centella-ampoule-30ml-" class="info text-center" title="SKIN1004 Madagascar Centella Ampoule 30ml"> <strong class="supplier"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SKIN1004</font></font></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SKIN1004 Madagascar Centella Ampoule 30ml</font></font></a>
Test 1:
page.xpath('//a[@class="info text-center"]')
Test 2:
page.xpath("//a[@class='info text-center']")
Test 3:
page.xpath('//a[@class="info.text-center"]')
Test 4:
page.xpath("//a[contains(@class, 'info') and contains(@class, 'text-center')]")
I did couple more tests too but I forgot to save the code. It will be great to know how to select elements with multiple classes using lxml.html.xpath().
a = page.xpath('//a[@class="info text-center"]') print(a[0].text)