【问题标题】:Extract HTML tag data of content of its first tag matches提取其第一个标签匹配内容的HTML标签数据
【发布时间】:2021-07-11 18:40:00
【问题描述】:

我刚刚开始学习美丽的汤,我正在从一个带有如下标签的网页中提取数据:

<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 >

我想要的是,如果标签有文本“总体评分:”,那么我计算 img['src']。 img['src'] 给我一个特定对象的评级。

我真的不知道该怎么做。我尝试了多种方法。

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

谁能帮忙。

谢谢

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    如果&lt;tr&gt;里面没有其他图片,你可以使用这个例子来获取计数:

    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))
    

    打印:

    Rating: 2
    

    编辑:获取更多信息:

    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)
    

    打印:

    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.
    --------------------------------------------------------------------------------
    

    【讨论】:

    • 谢谢安德烈。惊人的解决方案。我可以再问一个吗?因此,我试图根据患者的评论从druglib.com/ratingsreviews/abilify 中提取数据,我需要提取评级、益处和副作用。我看到,对于每种药物的评论,它都在一个表格中,我怎样才能一个一个地提取每个表格的上述详细信息?
    • 天哪。这太棒了。我正在尝试其他事情。谢啦。你太棒了。我会多练习的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多