Home > Mobile >  How to get the div before a specific div with css selector
How to get the div before a specific div with css selector

Time:11-11

There is probably a better way to do this, but I just need this to work for now before I can come up with a better solution. Im working on a webscraping application with Python and BeautifulSoup. I need to grab a specific div, but the placement of that div changes slightly on different pages (sometimes its the 3rd, sometimes the 4th, ect). There are no class tags or id tags on the div I want, but I did notice how there was always a div directly after the one I want, and that one has a id tag. It looks something like this:

<div id="main-container">
    <div></div>
    <div></div>
    <div>The div I want</div>
    <div id="point"></div>
    <div></div>
</div>

So im looking for something like this:

div#main-container > div:item-before(#point)

Is there any easy way to do this in CSS, or do I have to come up with a better solution?

CodePudding user response:

Find specific div using id or class and call find_previous() to get appropriate tag

html="""<div id="main-container">
    <div></div>
    <div></div>
    <div>The div I want</div>
    <div id="point"></div>
    <div></div>
</div>"""
soup=BeautifulSoup(html,"html.parser")
soup.find("div",attrs={"id":"main-container"}).find("div",attrs={"id":"point"}).find_previous()

Output:

<div>The div I want</div>

CodePudding user response:

You can access your div by nth selector in CSS file :

 div#main-container:nth-child(3){
   /*Your code*/
 }
  • Related