我如何使用for循环解析?



我的目标是解析"funpay.com"的报价页面。这必须很简单,因为所有的报价名称都在同一个类'tc-item'中。

然而,我不能使用bs4+请求,因为这个页面只有在你登录时才加载,这是我通过cookie (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)

基于相当薄弱的起点,我怀疑这是在迭代期间发生的错误,因此我将这样做。

为了不直接丢弃所有内容,在循环中检查要查找的元素是否可用,并输出相应的结果。

...
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)

最新更新