-5

can someone help me how to get value of "end" in this code. I am using beautifulsoup here.

<div class="number" color="secondary" data-reactid="72" end="4269" start="0" style="display:flex;align-content:space-between;font-size:1.6rem;line-height:2.2399999999999998rem;color:rgb(130, 130, 130);">0</div>
Jeyekomon
  • 2,878
  • 2
  • 27
  • 37

2 Answers2

1

Access the element using key-value. Here your key is the attribute name.

Ex:

from bs4 import BeautifulSoup
s = """<div class="number" color="secondary" data-reactid="72" end="4269" start="0" style="display:flex;align-content:space-between;font-size:1.6rem;line-height:2.2399999999999998rem;color:rgb(130, 130, 130);">0</div>"""
soup = BeautifulSoup(s, "html.parser")
print(soup.div["end"])

Output:

4269
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Thanks for quick answer.I just want to tell you that I have already declared r = requests.get(URL) and soup = BeautifulSoup(r.content). And this part of scraping is just a small part of my whole code. So do I need to write this code line again - soup = BeautifulSoup(s, "html.parser") ? – Rakesh Singh May 21 '18 at 07:05
  • No you do not have to declare this again. You should be able to get it from `soup = BeautifulSoup(r.text, "html.parser" )` – Rakesh May 21 '18 at 07:25
  • Thanks. I tried etree method. And it worked as well. – Rakesh Singh May 21 '18 at 08:25
  • You are welcome :) – Rakesh May 21 '18 at 08:32
0

Assuming your HTML is already sucked into soup:

soup.div['end']

hd1
  • 33,938
  • 5
  • 80
  • 91