如何从表格中刮出第二列

时间:2019-03-09 21:37:00

标签: python web-scraping beautifulsoup

我正在尝试从表的第二列抓取数据,但是失败了...

这是我的代码:

import bs4
import requests 
url = "https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom"`

data=requests.get(url)
soup=bs4.BeautifulSoup(data.text,'html.parser')
My_table = soup.find('table',{'class':'wikitable sortable'})
#print(My_table)
My_row = My_table.find_all('tr')
#print(My_row[1])
for row in My_row:
   data= (row.find('td')[1].text)
   print(data)

这是错误:

  

TypeError:“ int”对象不可下标

什么是最好的解决方案?

3 个答案:

答案 0 :(得分:0)

此代码似乎有效

import bs4
import requests

url = "https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom"

data = requests.get(url)
soup = bs4.BeautifulSoup(data.text, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable'})
rows = table.find_all('tr')
for i, row in enumerate(rows):
    if i > 0:
        for j, td in enumerate(row.children):
            if j == 3:
                print(td.text.strip())

答案 1 :(得分:0)

最近版本的Beautiful Soup(4.7+)使用名为Soup Sieve的软件包,该软件包提供选择器支持。我个人发现CSS选择器更容易用于此类事情。您可以在此处查看其支持的所有CSS选择器功能:https://facelessuser.github.io/soupsieve/selectors/

使用选择器实际上很容易解决这个问题。如果实现了CSS级别4 :nth-col支持,将会更加容易,但是如果没有以下支持,它仍然非常容易:

在这里,我们只需定位表并定位每一行中的第二个td元素。

import bs4
import requests

url = "https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom"

data = requests.get(url)
soup = bs4.BeautifulSoup(data.text, 'html.parser')

for td in soup.select('table.wikitable.sortable tr td:nth-child(2)'):
    print(td.text.strip())

截断的输出:

AB10, AB11, AB12, AB15, AB16, AB21, AB22, AB23, AB24, AB25, AB99non-geo
AB13
AB14
AB30
AB31
AB32
AB33
AB34
AB35
AB36
AB37
AB38
AB39
AB41
AB42
AB43
AB44
AB45
AB51
AB52
AB53
AB54
AB55
AB56
AL01 AL1, AL2, AL3, AL4
AL05 AL5
AL06 AL6, AL7shared
AL07 AL7shared, AL8
AL09 AL9, AL10
B001 B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15, B16, B17, B18, B19, B20, B21, B23, B24, B25, B26, B27, B28, B29, B30, B31, B32, B33, B34, B35, B36, B37, B38, B40, B42, B43, B44, B45, B46, B47, B48,B99non-geo
B049 B49, B50

答案 2 :(得分:0)

尝试下面的代码,它应该可以工作,它将返回第二列文本。

import bs4
import requests
url="https://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom"

data=requests.get(url)
soup=bs4.BeautifulSoup(data.text,'html.parser')
My_table = soup.find('table',{'class':'wikitable sortable'})
My_row = My_table.find_all('tr')
for row in My_row:
   data= row.find_next('td').find_next('td')
   print(data.text.strip())

输出:

AB10, AB11, AB12, AB15, AB16, AB21, AB22, AB23, AB24, AB25, AB99non-geo
AB10, AB11, AB12, AB15, AB16, AB21, AB22, AB23, AB24, AB25, AB99non-geo
AB13
AB14
AB30
AB31
AB32
AB33
AB34
AB35
AB36
AB37
AB38
AB39
AB41
AB42
AB43
AB44
AB45
AB51
AB52
AB53
AB54
AB55
AB56
AL01 AL1, AL2, AL3, AL4
AL05 AL5
AL06 AL6, AL7shared
AL07 AL7shared, AL8
AL09 AL9, AL10
B001 B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15, B16, B17, B18, B19, B20, B21, B23, B24, B25, B26, B27, B28, B29, B30, B31, B32, B33, B34, B35, B36, B37, B38, B40, B42, B43, B44, B45, B46, B47, B48,B99non-geo
B049 B49, B50
B060 B60, B61
B062 B62, B63
B064 B64
B065 B65
B066 B66, B67
B068 B68, B69
B070 B70, B71
B072  B72, B73, B74, B75, B76
B077 B77, B78, B79
B080 B80
B090 B90, B91, B92, B93, B94
B095 B95
B096 B96, B97, B98
BA01 BA1, BA2
BA03 BA3
BA04 BA4
BA05 BA5
BA06 BA6
BA07 BA7
BA08 BA8
BA09 BA9shared
BA09 BA9,non-geo shared[2] BA10
BA11
BA12
BA13
BA14
BA15
BA16
BA20, BA21, BA22
BB01 BB1, BB2, BB6
BB03 BB3
BB04 BB4
BB05 BB5
BB07 BB7
BB08 BB8
BB09 BB9
BB10, BB11, BB12
BB18, BB94non-geo
BD01 BD1, BD2, BD3, BD4, BD5, BD6, BD7, BD8, BD9, BD10, BD11, BD12, BD13, BD14, BD15, BD98,non-geo shared BD99non-geo
BD16, BD97non-geo
BD17, BD18, BD98non-geo shared
BD19
BD20, BD21, BD22
BD23, BD24shared
BD24shared
BF01 BF1non-geo
BH01 BH1, BH2, BH3, BH4, BH5, BH6, BH7, BH8, BH9, BH10, BH11
BH12, BH13, BH14, BH15, BH16, BH17
BH18
BH19
BH20
BH21
BH22
BH23
BH24
BH25
BH31
BL00 BL0,