BeautifulSoup Specify table column by number?

时光怂恿深爱的人放手 提交于 2019-12-11 09:59:50

问题


Using Python 2.7 and BeautifulSoup 4, I'm scraping song names from a table.

Right now the script finds links in the row of a table; how can I specify I want the first column?

Ideally I'd be able to switch numbers around to change which ones got selected.

Right now the code looks like this:

from bs4 import BeautifulSoup

import requests

r  = requests.get("http://evamsharma.finosus.com/beatles/index.html")

data = r.text

soup = BeautifulSoup(data)

for table in soup.find_all('table'):
    for row in soup.find_all('tr'):
        for link in soup.find_all('a'):
            print(link.contents)

How do I, in effect, index the <td> tags within each <tr> tag?

The URL in there right now is a page on my site where I basically copied the table source from Wikipedia to make the scraping a little simpler.

Thanks!

evamvid


回答1:


Find all td tags inside tr and get the one you need by index:

index = 2
for table in soup.find_all('table'):
    for row in soup.find_all('tr'):
        try:
            td = row.find_all('td')[index]
        except IndexError:
            continue
        for link in td.find_all('a'):
            print(link.contents)


来源:https://stackoverflow.com/questions/22973680/beautifulsoup-specify-table-column-by-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!