BeautifulSoup-如何从没有ID的表中查找值?

时间:2018-08-29 01:50:54

标签: python html web-scraping beautifulsoup

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

由于看不到特定的ID或类,因此不确定如何执行此操作。

谢谢

2 个答案:

答案 0 :(得分:0)

这将返回所有宽度为100%的表

您可以尝试:

tables = soup.find_all('table',width =“ 100%”)

using(SqlDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {
        var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result.
        // Do somthing with this rows string, for example to put them in to a list
        listDeclaredElsewhere.Add(myString);
    }
}

答案 1 :(得分:0)

通过在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

中输出所需的值
相关问题