Skip to content Skip to sidebar Skip to footer

Scraping Data- Attributes From Web Page

I am needing some assistance on using python to scrape some data- attributes form a site. I have tried using lxml and requests with no luck and have looked online and I found some

Solution 1:

you can use select. you can try it:

from bs4 import BeautifulSoup
html = """
<div class="card-entry" data-var1="0" data-var2="1" data-var3="20" data-var4="3" data-var5="9">… </div>
<div class="card-entry" data-var1="1" data-var2="2" data-var3="9" data-var4="2" data-var5="7">… </div>
<div class="card-entry" data-var1="2" data-var2="3" data-var3="1" data-var4="3" data-var5="3">…</div>
<div class="card-entry" data-var1="3" data-var2="4" data-var3="5" data-var4="2" data-var5="9">…</div> 
"""

soup = BeautifulSoup(html, "lxml")
data_var = soup.select('div[data-var5]')

for data in data_var:
    print("data-var5: " + data['data-var5'])

Output will be:

data-var5: 9data-var5: 7data-var5: 3data-var5: 9

Solution 2:

from bs4 import BeautifulSoup

html = """
<div class="card-entry" data-var1="0" data-var2="1" data-var3="20" data-var4="3" data-var5="9">… </div>
<div class="card-entry" data-var1="1" data-var2="2" data-var3="9" data-var4="2" data-var5="7">… </div>
<div class="card-entry" data-var1="2" data-var2="3" data-var3="1" data-var4="3" data-var5="3">…</div>
<div class="card-entry" data-var1="3" data-var2="4" data-var3="5" data-var4="2" data-var5="9">…</div> 
"""

soup = BeautifulSoup(html, "html.parser")
divs = soup.find_all("div", "card-entry")
for div in divs:
    print(div["data-var5"])

Post a Comment for "Scraping Data- Attributes From Web Page"