简体   繁体   中英

BeautifulSoup4 extract multiple data from TD tags within TR

Using beautifulsou 4 to scrape a HTML table.

To display values from one of the table rows and remove any empty td fields. The source being scraped shares classes=''

So is there any way to pull the data form just one row? using

data-name ="Georgia" in the html source below

Using: beautifulsoup4

Current code

 import bs4 as bs from urllib.request import FancyURLopener

class MyOpener(FancyURLopener):
    version = 'My new User-Agent'   # Set this to a string you want for your user agent

myopener = MyOpener()
sauce = myopener.open('')
soup = bs.BeautifulSoup(sauce,'lxml')

#table = soupe.table
table = soup.find('table')
table_rows = table.find_all_next('tr')

for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]
    print(row)

HTML SOURCE

        <tr>
          <td class="text--gray">

            <span class="save-button" data-status="unselected" data-type="country" data-name="Kazakhstan">&#9733;</span>

            Kazakhstan
          </td>

          <td class="text--green">
            81
          </td>

          <td class="text--green">
            9
          </td>

          <td class="text--green">
            12.5
          </td>

          <td class="text--red">
            0
          </td>
          <td class="text--red">
            0
          </td>
          <td class="text--red">
            0
          </td>
          <td class="text--blue">
            0
          </td>
          <td class="text--yellow">
            0
          </td>
        </tr>







        <tr>
          <td class="text--gray">

            <span class="save-button" data-status="unselected" data-type="country" data-name="Georgia">&#9733;</span>

            Georgia
          </td>

          <td class="text--green">
            75
          </td>

          <td class="text--green">
            0
          </td>

          <td class="text--green">
            0
          </td>

          <td class="text--red">
            0
          </td>
          <td class="text--red">
            0
          </td>
          <td class="text--red">
            0
          </td>
          <td class="text--blue">
            10
          </td>
          <td class="text--yellow">
            1
          </td>
        </tr>

Are you talking about something like:

tr.find_all('td',{'data-name' : True})

That should find any td that contains data name. I could be reading your question all wrong though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM