BeautifulSoup - Как найти значения из таблицы без идентификатора? - PullRequest
0 голосов
/ 29 августа 2018

Попытка очистить значения из строки «Нераспределенная прибыль» под заголовком «Британские основы земли» в https://uk.advfn.com/p.php?pid=financials&symbol=L%5EBLND

Не знаю, как это сделать, поскольку я не вижу определенного идентификатора или класса.

Спасибо

Ответы [ 2 ]

0 голосов
/ 29 августа 2018

Разобрался с рабочим решением, найдя определенное имя строки в исходном коде 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

0 голосов
/ 29 августа 2018

это возвращает все таблицы с шириной 100%

вы можете попробовать:

tables = soup.find_all ('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]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...