簡體   English   中英

逐行刮表? Python 網頁抓取

[英]Scraping the table row by row? Python Webscraping

如果您訪問此鏈接: https : //www.halton.ca/For-Residents/Food-Safety/Dinewise/Search-Directory-of-Food-Premises-Dinewise並單擊餐廳並查看此頁面。

在此處輸入圖片說明

我想抓取此表中的所有信息。

起初我嘗試使用:

driver.find_element_by_xpath('//*[@id="Form1"]/table[1]').text)

這確實獲得了我的表格部分信息。 但是,因為在“滿意”下,“檢查時更正”和“不適用”是圖像。 這種方法不允許我獲取它的文本。 因此我在考慮我可以獲取源鏈接​​並確定它是是還是否。

我的問題是如何逐行抓取這張表? 這是我的嘗試,我無法刮取“描述”部分

 # Get Areas of Assessment, Description then Satisfactory
        table =  driver.find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody')
        rows = table.find_elements_by_tag_name("tr")  # get all of the rows in the table
        
        content = []
        
        for row in range(1, len(rows)+1):
            # Get the columns (all the column 2)
            
            #areas of assessment
            a = rows[row].find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr[%s]/td[1]/b'%row).text
            content.append(a)
            print(content)
            print(a)
            #description
            b =rows[row].find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr[1]/td[2]/br').text
            print(b)
            #satisfactory
#             test =rows[row].find_element_by_id('chkFoodProtectedFromContamination_Satisfactory').get_attribute("src")
            print(test)

只需執行以下操作即可檢索圖像的 src。

c = rows[row].find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr[%s]/td[3]/img').getAttribute("src")
print(c)

暫無
暫無

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

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