【问题标题】:Web-scraping dynamic HTML page structure网页抓取动态 HTML 页面结构
【发布时间】:2020-07-30 11:22:31
【问题描述】:

我正在从事一个大型网页抓取项目,其中每个网页的 HTML 结构彼此不同。我想从网页上抓取产品描述,我正在使用 BeautifulSoup 包。

例如,我试图抓取的产品描述存储在 HTML 结构中:

<div class="product-description">
  <p> "Title" </p>
  <p> "Some content" </p>
  <p> "Product description" </p>
</div>


<div class="product-description">
  <p> "Title" </p>
  <p> "Product description" </p>
</div>

<div class="product-description">
  <p> "Title" </p>
  <p> "Some content" </p>
  <p> "Some content" </p>
  <p> "Product description" </p>
</div>


<div class="product-description">
  <p> "Title" </p>
  <p> "Some-content" </p>
  <p> "Some-content" </p>
  <p> "Some-content" </p>
  <p> "Product description" </p>
</div>

我编写了一个 for 循环,根据页面结构从 div 类“产品描述”中获取数据。我的示例代码sn-p:

requests = (grequests.get(url) for url in urls)
responses = grequests.imap(requests, grequests.Pool(1000))

for response in responses:

        html_soup = BeautifulSoup(response.text, 'html.parser')

        if html_soup.find('div',class_='product_description').next_element.next_sibling.next_sibling.next_sibling.next_sibling:
                product_description = html_soup.find('div',class_='product_description').next_element.next_sibling.next_sibling.next_sibling.next_sibling.text

        elif html_soup.find('div', class_='product-description').next_element.next_sibling.next_sibling.next_sibling:
                product_description = html_soup.find(
                  'div', class_='product_description').next_element.next_sibling.next_sibling.next_sibling.text

        elif html_soup.find('div', class_='product-description').next_element.next_sibling.next_sibling:
                product_description = html_soup.find(
                  'div', class_='product_description').next_element.next_sibling.next_sibling.text

        else:
                product_description = html_soup.find(
                  'div', class_='product_description').next_element.next_sibling.text

我希望 if 条件检查当前级别的 HTML 中是否有兄弟姐妹,如果没有则检查后续条件。但是,经过 3000 次迭代后,我得到一个 Attribute errorNonetype object has no attribute next_sibling。截图如下:

我知道必须有其他更简单的方法来处理这种动态页面结构。任何帮助将非常感激。提前致谢!

【问题讨论】:

  • 全部添加到数组中,弹出第一个元素(Title),然后弹出最后一个元素(Descr),剩下的就是内容了。
  • [i.text for i in soup.select('.product-description p:last-child')] 如果总是有产品描述并且它是最后一个 p。如果适用,将写为答案,但需要确认假设。
  • 我的回答对你有帮助吗???
  • 感谢 @Joshua Varghese 的 cmets 和帮助。所有这些建议都有效,但我刚刚意识到“产品描述”在所有网页的最后一个

    中并不一致。所以,我正在考虑删除

    中的所有 html 标签并使用正则表达式来提取所需的数据。如果需要任何帮助,我将发布一个新问题。再次感谢您!

标签: html python-3.x web-scraping beautifulsoup


【解决方案1】:

试试这个:

for i in soup.find_all('div',class_="product-description"):
    try:
        print(i.find_all('p')[-1].text)
    except:
        pass

这里的汤是:

<div class="product-description">
  <p> "Title" </p>
  <p> "Some content" </p>
  <p> "Product description" </p>
</div>


<div class="product-description">
  <p> "Title" </p>
  <p> "Product description" </p>
</div>

<div class="product-description">
  <p> "Title" </p>
  <p> "Some content" </p>
  <p> "Some content" </p>
  <p> "Product description" </p>
</div>


<div class="product-description">
  <p> "Title" </p>
  <p> "Some-content" </p>
  <p> "Some-content" </p>
  <p> "Some-content" </p>
  <p> "Product description" </p>
</div>

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多