bs4 python не находит текст - PullRequest
3 голосов
/ 11 марта 2019

У меня есть HTML-документ, который я взял через красивый суп.Выдержка HTML находится в нижней части этого вопроса.Я пользуюсь прекрасным супом и селеном.

Мне сказали, что мне разрешено извлекать столько данных в час, а когда я получаю эту страницу, то немного подожду (хороший час).

Вот как я пытаюсь извлечь данные:

def get_page_data(self):
    opts = Options()
    opts.headless = True
    assert opts.headless  # Operating in headless mode
    browser_detail = Firefox(options=opts)
    url = self.base_url.format(str(self.tracking_id))
    print(url)
    browser_detail.get(url)
    self.page_data = bs4(browser_detail.page_source, 'html.parser')
    Error_Check = 1 if len(self.page_data.findAll(text='Error Report Number')) > 0 else 0
    Error_Check = 2 if len(self.page_data.findAll(text='exceeded the maximum number of sessions per hour allowed')) > 0 else Error_Check
    print(self.page_data.findAll(text='waiting an hour and trying your query again')). ##<<--- The Problem is this line.
    print(self.page_data)
    return Error_Check

Проблема в этой строке:

print(self.page_data.findAll(text='waiting an hour and trying your query again')). ##<<--- The Problem is this line.

Код не может найти строку встраница.Что мне не хватает?Спасибо

<html><head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<link href="/CMPL/styles/ogm_style.css;jsessionid=rw9pc8-bncrIy_4KSZmJ8BxN2Z2hnKVwcr79Vho4-99gxTPrxNbo!-68716939" rel="stylesheet" type="text/css"/>
<body>
<!-- Content Area -->
<table style="width:100%; margin:auto;">
<tbody><tr valign="top">
<td class="ContentArea" style="width:100%;">
<span id="messageArea">
<!-- /tiles/messages.jsp BEGIN -->
<ul>
</ul><b>
</b><table style="width:100%; margin:auto; white-space: pre-wrap; text-align: left;">
<tbody><tr><td align="left"><b><li><font color="red"></font></li></b></td>
<td align="left"><font color="red">You have exceeded the maximum number of sessions per hour allowed for the public queries. You may still access the public</font></td>
</tr>
<tr><td><font color="red"><li style="list-style: none;"></li></font></td>
<td align="left"><font color="red">queries by waiting an hour and trying your query again. The RRC public queries are provided to facilitate online research and are not intended to be accessed by automated tools or scripts. For questions or concerns please contact the RRC HelpDesk at helpdesk@rrc.state.tx.us or 512-463-7229</font></td>
</tr>
</tbody></table>
<p>....more html...</p>
</body></html>

Ответы [ 2 ]

2 голосов
/ 11 марта 2019

Вы можете использовать следующий селектор CSS

tr:last-child:not([valign])

т.е.

from bs4 import BeautifulSoup as bs
html = '''yourHTML'''    
soup = bs(html, 'lxml')   
item = soup.select_one('tr:last-child:not([valign])')
print(item.text)

Если это возвращает более одного элемента, вы можете зациклить фильтрацию списка для элементов, содержащих интересующую строку. Вы можете ограничиться только селектором td и сделать что-то подобное.

items = soup.select('tr:last-child:not([valign])')
for item in items:
    if 'queries by waiting an hour' in item.text:
        print(item.text)

BeautifulSoup 4.7.1

1 голос
/ 11 марта 2019

Я не уверен, что это то, что вы ищете, но если вы:

html = [your code above]
from bs4 import BeautifulSoup as bs4
soup = bs4(html, 'lxml')
data = soup.find_all('font', color="red")
data[3].text

Выход:

'queries by waiting an hour and trying your query again. The RRC public queries are provided to facilitate online research and are not intended to be accessed by automated tools or scripts. For questions or concerns please contact the RRC HelpDesk at helpdesk@rrc.state.tx.us or 512-463-7229'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...