Answer a question

I am using bs4 with python and trying to fetch data from a web page. Link I used inspect element over the info i want, but both have same tag,class.

             <a class="cell__value" data-tracker-action="click" data-tracker-label="information_technology.01" href="/markets/sectors/information-technology">
             Information Technology
            </a>
           </div>
           <div class="cell__return">
            <div class="cell__label">
             % Price Change
            </div>
            <div class="cell__value" data-type="better">
             +0.05%
            </div>
           </div>
          </div>
          <div class="cell">
           <div class="cell__name">
            <div class="cell__label">
             Industry
            </div>
            <a class="cell__value" data-tracker-action="click" data-tracker-label="information_technology.02" href="/markets/sectors/information-technology">
             Software &amp; Services
            </a>
           </div>
           <div class="cell__return">
            <div class="cell__label">
             % Price Change
            </div>
            <div class="cell__value" data-type="worse">
             -0.04%
            </div>
           </div>
          </div>
         </div>

I am doing it this way:

sect= soup.find("a",{"data-tracker-label":"information_technology.01"})
print sect.text
sect_per= soup.find("div",{"data-type":"worse"or"better"})
print sect_per.text
ind=soup.find("a",{"data-tracker-label":"information_technology.02"})
print ind.text
ind_per=soup.find("div",{"div",{"data-type":"worse"or"better"})
print ind_per

both print ind_per and print ind_per are giving me same result because of same class and tag

i need to extract +0.05% and -0.04% respectively.

Please suggest me way to do it.

Answers

or returns the left operand if the left operand is truth value (non-empty string for string):

>>> "worse" or "better"
'worse'

So, the following line:

ind_per = soup.find("div",{"div",{"data-type":"worse" or "better"})

is basically doing same with:

ind_per = soup.find("div",{"div",{"data-type":"worse"})

You need to query them separately:

ind_per = soup.find("div",{"div",{"data-type": "worse"})
print ind_per
ind_per = soup.find("div",{"div",{"data-type": "better"})
print ind_per

or using for loop:

for data_type in ('worse', 'better'):
    ind_per = soup.find("div",{"div",{"data-type": data_type})
    print ind_per
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐