Python -如何处理异常?



我的代码从一个网页中收集了一堆url,然后把它们放到一个列表中。

一旦进入列表,它就逐一进入每个列表,然后执行刮取操作。

然而,一些网页,一旦被访问,有一个空白页,阻止代码执行其余的url。

我怎么能添加一个异常或我的代码,如果发生这种情况,我可以只是绕过网页,继续进入下一个URL?

from selenium import webdriver
import time
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
import pandas as pd
import requests
dataf=[]
val=[]
baseurl='https://careers.abbvie.com/'
endurl='?lang=en-us&previousLocale=en-US'
for x in range(1,89):
driver.get(f'https://careers.abbvie.com/abbvie/jobs?page={x}&categories=Administrative%20Services%7CBusiness%20Development%7CGeneral%20Management%7CHEOR%2FMarket%20Access%7CInformation%20Technology%7CMarketing%7CMedical%7CRegulatory%20Affairs%7CSales%7CSales%20Support')
time.sleep(7)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
eachRow = soup.find_all('p', class_='job-title')
for link in eachRow:
for links in link.find_all('a',href=True):
val.append(baseurl+links['href']+endurl)
for b in val:
try:
driver.get(b)
time.sleep(3)
page_source = driver.page_source
title=driver.find_element_by_xpath('//*[@id="jibe-container"]/div[2]/div/div/h1').text
location=driver.find_element_by_xpath('//*[@id="header-locations"]/span').text
categories=driver.find_element_by_xpath('//*[@id="header-categories"]/span').text
jobID=driver.find_element_by_xpath('//*[@id="header-req_id"]/span').text
dict={"Title":title,"location":location,"categories":categories,"jobID":jobID,"URL":b}
dataf.append(dict)
except:
print("hello")
df=pd.DataFrame(dataf)
df.to_csv('restasis.csv')

您需要做的是用try/except块包装可能抛出异常的行

try:
# do something that may throw
except Exception:
# decide what to do (ignore? / print?)

相关内容

  • 没有找到相关文章

最新更新