[英]Extract specific value from a table using Beautiful Soup (Python)
我在 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)
可能你误用find_all
function,试试这个解决方案:
style=table.find_all('tr')[3]
style=style.find_all('td')[1].text
print(style)
它会给你预期的 output
也可以做类似的事情:
import bs4
import requests
style_data = []
url = "https://gis.vgsi.com/portsmouthnh/Parcel.aspx?pid=38919"
soup = bs4.BeautifulSoup(requests.get(url).content, 'html.parser')
# select the first `td` tag whose text contains the substring `Style:`.
row = soup.select_one('td:-soup-contains("Style:")')
if row:
# if that row was found get its sibling which should be that vlue you want
home_style_tag = row.next_sibling
style_data.append(home_style_tag.text)
一些笔记
select_one
依赖于表格总是以某种方式排序的事实,如果不是这种情况,请使用select
并遍历结果以找到文本恰好为'Style:'
的bs4.Tag
,然后获取其下一个兄弟使用select
:
rows = soup.select('td:-soup-contains("Style:")')
row = [r for r in rows if r.text == 'Style:']
home_style_text = row.text
您可以使用 CSS 选择器:
#MainContent_ctl01_grdCns tr:nth-of-type(4) td:nth-of-type(2)
其中将 select "MainContent_ctl01_grdCns"
id
,第四个<tr>
,第二个<td>
。
要使用 CSS 选择器,请使用.select()
方法而不是find_all()
。 或select_one()
而不是find()
。
import requests
from bs4 import BeautifulSoup
URL = "https://gis.vgsi.com/portsmouthnh/Parcel.aspx?pid=38919"
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
print(
soup.select_one(
"#MainContent_ctl01_grdCns tr:nth-of-type(4) td:nth-of-type(2)"
).text
)
Output:
Townhouse End
您可以在td
上使用:contains
来获取具有innerText
"Style" 的节点,然后使用具有td
类型选择器的相邻兄弟组合器来获取相邻的td
值。
import bs4, requests
pagedata = requests.get("https://gis.vgsi.com/portsmouthnh/Parcel.aspx?pid=38919")
cleanpagedata = bs4.BeautifulSoup(pagedata.text, 'html.parser')
print(cleanpagedata.select_one('td:contains("Style") + td').text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.