我是靠谱客的博主 花痴绿草,这篇文章主要介绍python3 beautifulsoup 表格指定行_BeautifulSoup按数字指定表格列?,现在分享给大家,希望可以做个参考。

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

tags within each 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

解决方案

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)

最后

以上就是花痴绿草最近收集整理的关于python3 beautifulsoup 表格指定行_BeautifulSoup按数字指定表格列?的全部内容,更多相关python3内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部