python - 如何从表中刮取第二列
问题描述
我正在尝试从表的第二列中抓取数据但失败了...
这是我的代码:
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”对象不可下标
什么是最好的解决方案?
解决方案
这段代码似乎工作
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())
推荐阅读
- huggingface-transformers - Rasa 与 HuggingFace 的集成管道
- javascript - 如何在单击时将函数中的变量作为全局变量返回
- performance - 如何去除 Faster RCNN 中的误检(误报)
- oracle - 在Snowflake sql中我们可以调用Oracle的Sequence函数吗
- powershell - GCloud powershell 命令抛出错误,但在 cmd 中工作正常
- macos - 有没有办法在 Mac vi 编辑器中选择所有过滤后的字符串?
- python - 按具体日期排序?数字?列表中包含的列表
- javascript - 如何在 Vue3 compositionAPI 中触发 $forceUpdate?
- wordpress - 自定义 WordPress PHP 代码以在帖子中显示其他帖子破坏了我的评论
- r - 如何在地图上使用 GCIntermediate 在太平洋上为航线制作弧线?