亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

網頁抓取動態 HTML 頁面結構

網頁抓取動態 HTML 頁面結構

慕俠2389804 2024-01-03 14:43:54
我正在從事一個大型網頁抓取項目,其中每個網頁的 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 類“產品描述”獲取數據。我的示例代碼片段: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 error一句話Nonetype object has no attribute next_sibling。下面附上截圖:我知道一定有其他更簡單的方法來處理這個動態頁面結構。任何幫助將非常感激。提前致謝!
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

嘗試這個:


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>


查看完整回答
反對 回復 2024-01-03
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號