Właśnie zacząłem uczyć się pięknej zupy i wyodrębniam dane ze strony internetowej, która ma takie znaczenie:

<tr > < td class = "review2"
width = "23%" > Overall rating: < /td><td>  </td > < td class = "review3" > < img hight = "18px"
src = "/img/red_star.gif" width = "18px" / > < img hight = "18px" src = "/img/red_star.gif" width = "18px" / > < /td></tr >
<tr > < td class = "review2" > Effectiveness: < /td><td>  </td > < td class = "review3" > Highly Effective < /td></tr >

Chcę, że jeśli tag ma tekst "Ogólna ocena:" Wtedy policzę img ['src']. Img ['src'] daje mi ocenę konkretnego obiektu.

Naprawdę nie wiem, jak postępować. Próbowałem wiele sposobów.

tr = soup.find_all('tr')
td = []
for i in tr:
    td.append(i.find_all('td',class_='review3'))

Czy ktoś może pomóż.

Dzięki

1
Ash15 16 kwiecień 2021, 21:15

1 odpowiedź

Najlepsza odpowiedź

Jeśli wewnątrz <tr> nie są innymi obrazami, możesz użyć tego przykładu, aby uzyskać liczbę:

from bs4 import BeautifulSoup


html_doc = """
<tr> <td class = "review2"
width = "23%"> Overall rating: </td><td>  </td> <td class = "review3"> <img hight = "18px"
src = "/img/red_star.gif" width = "18px" /> <img hight = "18px" src = "/img/red_star.gif" width = "18px" /> </td></tr>
<tr> <td class = "review2"> Effectiveness: </td><td>  </td> <td class = "review3"> Highly Effective </td></tr>
"""

soup = BeautifulSoup(html_doc, "html.parser")

imgs = soup.select('td:contains("Overall rating:") ~ td > img')
print("Rating:", len(imgs))

Wydruki:

Rating: 2

Edytuj: Aby zeskrobać więcej informacji:

import requests
from bs4 import BeautifulSoup


url = "http://www.druglib.com/ratingsreviews/abilify/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for h2 in soup.select("h2"):
    print(h2.text)
    print(
        "Rating:",
        len(
            h2.find_next(class_="review3").find_all(
                lambda tag: tag.name == "img"
                and "red_star" in tag.get("src", "")
            )
        ),
    )
    print(
        "Review:",
        h2.find_next("td", text="Benefits:").find_next(class_="review3").text,
    )
    print(
        "Side effects:",
        h2.find_next(
            lambda tag: tag.name == "td" and "Reported Results" in tag.text
        )
        .find_next("td", text="Side effects:")
        .find_next(class_="review3")
        .text,
    )
    print("-" * 80)

Wydruki:

Abilify review by  26 year old female patient
Rating: 3
Review:  I didn't notice any benefit at all. Supposedly, it was to help keep my mood balanced.  More than anything, I was focused on the very apparent and prevalent side effects.
Side effects:  A very uncomfortable inner restlessness was the worst side effect.  I felt like I had to constantly get up and move around, but it didn't even help.  I couldn't sit still, had major problems sleeping at night, AND started gaining weight.
--------------------------------------------------------------------------------
Abilify review by  29 year old female patient
Rating: 8
Review:  I had severe depression with agitation and mixed-state bipolar hypomania. I was insomniac. I took abilify 5mg for 3 mos, and then I upped it to 10mg. 
Side effects:  I became drowsy, however, with adequate sleep (10 hrs) there were no hangover effects, unlike zyprexa, for instance. I also experienced some cognitive blunting- that is to say that I could not think very well on my feet
--------------------------------------------------------------------------------
Abilify review by  43 year old female patient
Rating: 10
Review:  Within 1 week of taking the cocktail of Abilify and Lexapro, a extremely unorganized person....became organized. I am now able to remember appointment times, keep up with my daily responsibilities...it has been a lifesaver
Side effects:  no side effects have been noticed
--------------------------------------------------------------------------------
Abilify review by  50 year old female patient
Rating: 2
Review:  While on abilify I can honestly say the depression resolved,
Side effects:   but it caused memory loss and again an incident involving retail theft. The retail theft occured while on just 5 mg of abilify. It effected my judgement/reasoning skills because I did not tell my doctor & allowed him to continue to increase dosage up to 15mg when I became expremely restless. Could not set down, paced for hours & called my doctor about this. Also during the 2 months I was taking this drug I charged a little over 15,000 on my credit cards that had no balance on them previously. If one did it was under $300. I still am currently unemployed & am having difficulty finding employement as a RN, due to my criminal record within the last year. If I had problems before taking antipsychotics, they were nothing compared to what I have now. 
--------------------------------------------------------------------------------
Abilify review by  50 year old male patient
Rating: 2
Review:  None due to the short time taking drug.
Side effects:  Headache first morning at 4AM that was relieved with Excedrin.  Migraine headache with vomiting that lasted 14 hours.  Couldn't go to work that day and stopped taking it.
--------------------------------------------------------------------------------
Abilify review by  52 year old female patient
Rating: 6
Review:  I've only been on it for a week but I've noticed a change already. I am more awake and it seems as though a fog has been lifted. I am thinking more clearly and don't feel so down.
Side effects:  None so far.
--------------------------------------------------------------------------------
Abilify review by  52 year old female patient
Rating: 6
Review:  I've only been on it for a week but I've noticed a change already. I am more awake and it seems as though a fog has been lifted. I am thinking more clearly and don't feel so down.
Side effects:  None so far.
--------------------------------------------------------------------------------
Abilify review by  52 year old female patient
Rating: 6
Review:  I've only been on it for a week but I've noticed a change already. I am more awake and it seems as though a fog has been lifted. I am thinking more clearly and don't feel so down.
Side effects:  None so far.
--------------------------------------------------------------------------------
1
Andrej Kesely 17 kwiecień 2021, 08:55