簡體   English   中英

如何使用 for 循環進行解析?

[英]How do I parse using for-loop?

我的目標是解析“funpay.com”的報價頁面。 它必須很簡單,因為所有報價名稱都在同一個 class 'tc-item'中。

但是我不能使用 bs4+requests,因為這個頁面只有在你登錄時才會加載,我是通過 cookies ( selenium+pickle ) 登錄的。

完全不知道怎么做,所以我會很感激任何提示。

我試過的代碼:

driver.get("https://funpay.com/orders/trade")
soup = bs(driver.page_source, 'html.parser')

try:

    paid = soup.find_all('a', class_='tc-item')
    for sold in paid:
        title = sold.find('div', class_='tc-order') # inside 'a', 
                                                    # prints the code of offer
        print(title)

except Exception as ex:
    print(ex)

基於相當薄的起點,我懷疑這是在迭代過程中發生的錯誤,所以這就是我要做的。

為了不直接丟棄所有內容,請在循環內部檢查您要查找的元素是否可用,然后讓 output 得到相應的結果。

...
soup = bs(driver.page_source, 'html.parser')
paid = soup.find_all('a', class_='tc-item')

for sold in paid:
    title = sold.find('div', class_='tc-order').text if sold.find('div', class_='tc-order') else 'no element found'
    print(title)

暫無
暫無

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

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