I've read in some HTML from a website that I'm trying to parse into a soup object. After that I have extracted the body of the HTML which I have looked at and seen that it does contain the items that I'm looking for.
The items that I'm looking for are contained inside <div>
tags, with class = 'listitem artikal obicniArtikal imaHover-disabled i index'.
Now I'm trying to do do:
r = requests.get('https://www.olx.ba/pretraga?trazilica=golf+2', timeout=5)
soup = BeautifulSoup(r.text, 'html.parser')
body = soup.find('div', attrs={'id':'rezultatipretrage'})
all_items = body.find_all('div', class_='listitem artikal obicniArtikal imaHover-disabled i index')
print(all_items)
all_items
returns an empty list even though the tags are present in the body
.
I would be sincerely grateful if anybody would tell me what's going on.
To select tag that contains all specified classes, you can use BeautifulSoup's select()
method.
soup.select('div#rezultatipretrage div.listitem.artikal.obicniArtikal.imaHover-disabled.i.index')
will select all <div>
tags with class listitem artikal obicniArtikal imaHover-disabled i index
that are under <div>
tag with id="rezultatipretrage"
:
from bs4 import BeautifulSoup
import requests
r = requests.get('https://www.olx.ba/pretraga?trazilica=golf+2', timeout=5)
soup = BeautifulSoup(r.text, 'lxml')
all_items = soup.select('div#rezultatipretrage div.listitem.artikal.obicniArtikal.imaHover-disabled.i.index')
for item in all_items:
print(item.p.text)
print('-' * 80)
Will print:
VW Golf 6 2.0 TDI XENON-NAVI-KAMERA-KOZA
--------------------------------------------------------------------------------
Golf 5 2.0 tdi. Sport line. bosch dizne
--------------------------------------------------------------------------------
VW GOLF 7 2.0 TDI DSG , ID: 086
--------------------------------------------------------------------------------
VW GOLF 5 2.0 TDI, 2005 god. Registrovan
--------------------------------------------------------------------------------
Golf 5 2.0 DIZEL SDI TEK REGISTROVAN
--------------------------------------------------------------------------------
Volkswagen Golf 6 2.0 TDI GTI-GTD-R LINE
--------------------------------------------------------------------------------
VW GOLF 5,2.0 TDI,103 KW,04 G.P,6 BRZ.MOTOR U KVARU
--------------------------------------------------------------------------------
Golf 6 2.0 TDI
--------------------------------------------------------------------------------
VW GOLF 7 2.0tdi 110kw 150ks mod 2014g
--------------------------------------------------------------------------------
Golf 2 1.6Dizel reg do 12 2018god
--------------------------------------------------------------------------------
VW Golf 2.0 ekstra stanje
--------------------------------------------------------------------------------
Golf 2 DIZEL 4 VRATA
--------------------------------------------------------------------------------
Golf 2
--------------------------------------------------------------------------------
VW GOLF 5 V 2.0 TDI CLIMATRONIC
--------------------------------------------------------------------------------
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.