簡體   English   中英

如何使用美麗的湯 python 將 Div 中的所有詳細信息導出到 excel/csv?

[英]How to export all the Details in the Div using Beautiful soup python to excel/csv?

我是 Soup/python 的新手,我正在嘗試查找數據。 我的網站結構看起來像這樣。 在此處輸入圖片說明

如果我打開 Divclass border class它看起來像這樣。(下圖)

我做了這樣的事情:

for  P in soup.find_all('p', attrs={'class': 'bid_no pull-left'}) :
  print(P.find('a').contents[0])

在此處輸入圖片說明

一個 Div 結構看起來像
每頁大約有 10 個 div
我想在其中提取Items,Quantity Require,Bid number,End date 請幫我

<div class="border block " style="display: block;">
    <div class="block_header">
        <p class="bid_no pull-left"> BID NO: <a style="color:#fff !important" href="/showbidDocument/1844736">GEM/2020/B/763154</a></p> 
        <p class="pull-right view_corrigendum" data-bid="1844736" style="display:none; margin-left: 10px;"><a href="#">View Corrigendum</a></p>

         <div class="clearfix"></div>
    </div>

    <div class="col-block">
        <p><strong style="text-transform: none !important;">Item(s): </strong><span>Compatible Cartridge</span></p>
        <p><strong>Quantity Required: </strong><span>8</span></p>

        <div class="clearfix"></div>
    </div>
    <div class="col-block">
        <p><strong>Department Name And Address:</strong></p>
        <p class="add-height">
            Ministry Of Railways<br> Na<br> South Central Railway N/a
        </p>
        <div class="clearfix"></div>
    </div>
    <div class="col-block">
        <p><strong>Start Date: </strong><span>25-08-2020 02:54 PM</span></p>
        <p><strong>End Date: </strong><span>04-09-2020 03:00 PM</span></p>
        <div class="clearfix"></div>

    </div>


    <div class="clearfix"></div>
</div>

錯誤圖片

錯誤 在此處輸入圖片說明

在此處輸入圖片說明

嘗試使用requests美麗的湯的以下方法。 我已經使用從網站獲取的 URL 創建了腳本,然后創建了一個動態 URL 來遍歷每個頁面以獲取數據。

腳本到底在做什么:

  1. 第一個腳本將創建一個 URL,其中page_no查詢字符串參數將在每次遍歷完成后遞增 1。

  2. 請求將使用get方法從創建的 URL 中獲取數據,然后該數據將傳遞給 Beautiful Soup 以使用lxml解析 HTML 結構。

  3. 然后從解析的數據腳本中搜索實際存在數據的div

  4. 最后對每一頁的所有div文本數據逐個循環。

     ```python import requests from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) from bs4 import BeautifulSoup as bs def scrap_bid_data(): page_no = 1 #initial page number while True: print('Hold on creating URL to fetch data...') URL = 'https://bidplus.gem.gov.in/bidlists?bidlists&page_no=' + str(page_no) #create dynamic URL print('URL cerated: ' + URL) scraped_data = requests.get(URL,verify=False) # request to get the data soup_data = bs(scraped_data.text, 'lxml') #parse the scraped data using lxml extracted_data = soup_data.find('div',{'id':'pagi_content'}) #find divs which contains required data if len(extracted_data) == 0: # **if block** which will check the length of extracted_data if it is 0 then quit and stop the further execution of script. break else: for idx in range(len(extracted_data)): # loops through all the divs and extract and print data if(idx % 2 == 1): #get data from odd indexes only because we have required data on odd indexes bid_data = extracted_data.contents[idx].text.strip().split('\\n') print('-' * 100) print(bid_data[0]) #BID number print(bid_data[5]) #Items print(bid_data[6]) #Quantitiy Required print(bid_data[10] + bid_data[12].strip()) #Department name and address print(bid_data[16]) #Start date print(bid_data[17]) #End date print('-' * 100) page_no +=1 #increments the page number by 1 scrap_bid_data() ```

實際代碼

在此處輸入圖片說明

輸出圖像

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM