我环顾了一下Stackoverflow,大多数指南似乎都非常详细地介绍了如何从表中提取所有数据。但是,我只需要提取一个,似乎无法从表中提取特定的值
刮取链接:
https://gis.vgsi.com/portsmouthnh/Parcel.aspx?pid=38919
我希望从链接中的表中提取“样式”值
代码:
import bs4
styleData=[]
pagedata = requests.get("https://gis.vgsi.com/portsmouthnh/Parcel.aspx?pid=38919")
cleanpagedata = bs4.BeautifulSoup(pagedata.text, 'html.parser')
table=cleanbyAddPD.find('div',{'id':'MainContent_ctl01_panView'})
style=table.findall('tr')[3]
style=style.findall('td')[1].text
print(style)
styleData.append(style)
您可以使用CSS选择器:
#MainContent_ctl01_grdCns tr:nth-of-type(4) td:nth-of-type(2)
它将选择},第四个
"MainContent_ctl01_grdCns"
{<tr>
,第二个<td>
要使用CSS选择器,请使用^{} 方法而不是
find_all()
。或者select_one()
而不是find()
输出:
可能您误用了
find_all
函数,请尝试以下解决方案:它将为您提供预期的输出
还可以执行以下操作:
几张便条
select_one
依赖于这样一个事实,即表总是以某种方式排序,如果不是这样,则使用select
并遍历结果以找到其文本正好是'Style:'
的bs4.Tag
,然后获取其下一个同级使用
select
:相关问题 更多 >
编程相关推荐