select 使用 python 和 beautifulsoup 的一组表格下的一组特定单元格
select a specific set of cell under a set of tables using python and beautifulsoup
- 假设有 N 个网页。
- 每个网页都有一个或多个table。 table 的共同点是它们的 class 相同,请考虑 "table_class."
- 我们需要每个table.
同一栏[第三栏,标题为标题]下的内容
- 内容含义,所有行中第三列的 href links。
- 有些行可能只是纯文本,有些行可能包含 href link。
您应该在单独的一行中打印每个 href link,一个接一个。
使用属性过滤无效,因为某些标签具有不同的属性。单元格的位置是唯一可用的提示。
如何编码?
考虑网页的这两个 link:
http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2014
http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2013
考虑 table: wikitable
必填内容:标题
列的 href links
我为一页尝试的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup, SoupStrainer
content = urlopen("http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2015").read()
filter_tag = SoupStrainer("table", {"class":"wikitable"})
soup = BeautifulSoup(content, parse_only=filter_tag)
for sp in soup.find_all('tr'):
for bt in sp.find_all('td'):
for link in bt.find_all('a'):
print(link.get("href"))
print()
这个想法是用 wikitable
class 遍历每个 table
;对于每个 table
直接在 i
标签内直接在 td
内直接在 tr
内找到链接:
import requests
from bs4 import BeautifulSoup
url = "http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2014"
soup = BeautifulSoup(requests.get(url).content)
# iterate over tables
for table in soup.select('table.wikitable.sortable'):
# get the table header/description, continue if not found
h3 = table.find_previous_sibling('h3')
if h3 is None:
continue
print h3.text
# get the links
for link in table.select('tr > td > i > a'):
print link.text, "|", link.get('href', '')
print "------"
打印(为了清楚起见,还打印 table 个名字):
January 2014–june 2014[edit]
Celebrity | /wiki/Celebrity
Kshatriya | /wiki/Kshatriya
1: Nenokkadine | /wiki/1:_Nenokkadine
...
Oohalu Gusagusalade | /wiki/Oohalu_Gusagusalade
Autonagar Surya | /wiki/Autonagar_Surya
------
July 2014 – December 2014[edit]
...
O Manishi Katha | /wiki/O_Manishi_Katha
Mukunda | /wiki/Mukunda
Chinnadana Nee Kosam | /wiki/Chinnadana_Nee_Kosam
------
- 假设有 N 个网页。
- 每个网页都有一个或多个table。 table 的共同点是它们的 class 相同,请考虑 "table_class."
- 我们需要每个table. 同一栏[第三栏,标题为标题]下的内容
- 内容含义,所有行中第三列的 href links。
- 有些行可能只是纯文本,有些行可能包含 href link。
您应该在单独的一行中打印每个 href link,一个接一个。
使用属性过滤无效,因为某些标签具有不同的属性。单元格的位置是唯一可用的提示。
如何编码?
考虑网页的这两个 link:
http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2014 http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2013
考虑 table: wikitable
必填内容:标题
列的 href links我为一页尝试的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup, SoupStrainer
content = urlopen("http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2015").read()
filter_tag = SoupStrainer("table", {"class":"wikitable"})
soup = BeautifulSoup(content, parse_only=filter_tag)
for sp in soup.find_all('tr'):
for bt in sp.find_all('td'):
for link in bt.find_all('a'):
print(link.get("href"))
print()
这个想法是用 wikitable
class 遍历每个 table
;对于每个 table
直接在 i
标签内直接在 td
内直接在 tr
内找到链接:
import requests
from bs4 import BeautifulSoup
url = "http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2014"
soup = BeautifulSoup(requests.get(url).content)
# iterate over tables
for table in soup.select('table.wikitable.sortable'):
# get the table header/description, continue if not found
h3 = table.find_previous_sibling('h3')
if h3 is None:
continue
print h3.text
# get the links
for link in table.select('tr > td > i > a'):
print link.text, "|", link.get('href', '')
print "------"
打印(为了清楚起见,还打印 table 个名字):
January 2014–june 2014[edit]
Celebrity | /wiki/Celebrity
Kshatriya | /wiki/Kshatriya
1: Nenokkadine | /wiki/1:_Nenokkadine
...
Oohalu Gusagusalade | /wiki/Oohalu_Gusagusalade
Autonagar Surya | /wiki/Autonagar_Surya
------
July 2014 – December 2014[edit]
...
O Manishi Katha | /wiki/O_Manishi_Katha
Mukunda | /wiki/Mukunda
Chinnadana Nee Kosam | /wiki/Chinnadana_Nee_Kosam
------