如何从没有id的表中找到值?

2024-09-28 19:26:24 发布

您现在位置:Python中文网/ 问答频道 /正文

试图从https://uk.advfn.com/p.php?pid=financials&symbol=L%5EBLND标题下的“英国土地基本面”下的“留存利润”行中提取价值

不知道该怎么做,因为我看不到具体的ID或类。在

谢谢


Tags: httpscomid标题symbolpidphp土地
2条回答

通过在HTML源代码中找到特定的行名,并迭代该行中的表值,得出一个可行的解决方案:

def foo(ticker):
    response = requests.get('https://uk.advfn.com/p.php?pid=financials&symbol=L%5E{}'.format(ticker))
    soup = bs.BeautifulSoup(response.text, 'lxml')
# find all 'a' tags
    for a in soup.find_all("a"):
    # find the specific row we want, there is only one instance of the a tag with the text 'retained profit' so this works
        if a.text == "retained profit":
        # iterate through each value in the row
            for val in a.parent.find_next_siblings():
            # return only the values in bold (i.e. those tags with the class='sb' in this case)
                for class_ in val.attrs['class']:
                    if class_ == 'sb':
                        print(val.text)

从表中输出所需值:D

此返回宽度为100%的所有表

您可以尝试:

表=汤。找你所有人('table',width=“100%”)

for tr in table.find_all("tr"):
  for key,value in enumerate(tr.find_all("td")):
    anchor = value.find_all("a",class="Lcc")
      if "Retained Profit PS" in anchor.text:
        return  tr.find_all("td")[key+1]

相关问题 更多 >