【问题标题】:Unable to loop through navigable string with BeautifulSoup CSS Selector无法使用 BeautifulSoup CSS 选择器遍历可导航字符串
【发布时间】:2020-10-06 12:28:59
【问题描述】:

我想提取下面p标签内的内容。

<section id="abstractSection" class="row">
<h3 class="h4">Abstract<span id="viewRefPH" class="pull-right hidden"></span>
</h3>
<p> Variation of the  (<span class="ScopusTermHighlight">EEG</span>), has functional and. behavioural effects in sensory <span class="ScopusTermHighlight">EEG</span We can interpret our. Individual <span class="ScopusTermHighlight">EEG</span> text to extract <span class="ScopusTermHighlight">EEG</span> power level.</p>
</section>

一行Selenium如下,

document_abstract = WebDriverWait(self.browser, 20).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@id="abstractSection"]/p'))).text

可以轻松提取p标签内容并提供如下输出:

Variation of the EEG, has functional and. behavioural effects in sensoryEEG. We can interpret our. Individual EEG text to extract EEG power level.

不过,出于速度考虑,我想使用BeautifulSoup

通过引用css选择器(即#abstractSection)测试了以下bs

url = r'scopus_offilne_specific_page.html'
with open(url, 'r', encoding='utf-8') as f:
    page_soup = soup(f, 'html.parser')
    home=page_soup.select_one('#abstractSection').next_sibling
    for item in home:
        for a in item.find_all("p"):
            print(a.get_text())

但是,编译器返回以下错误:

AttributeError: 'str' 对象没有属性 'find_all'

此外,由于Scopus 需要登录ID,因此可以使用通过link 访问的离线html 来重现上述问题。

请问我哪里做错了,感谢任何见解

【问题讨论】:

    标签: selenium web-scraping beautifulsoup scrapy


    【解决方案1】:

    感谢OP,上面的问题显然可以简单的解决如下

    document_abstract=page_soup.select('#abstractSection > p')[0].text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2015-01-03
      • 2022-11-11
      • 2018-09-12
      相关资源
      最近更新 更多