python遇到错误跳过_使用BeautifulSoup跳过错误404

我想用beauthulsoup删除一些网址。我正在抓取的URL来自googleanalytics API调用,其中一些不能正常工作,所以我需要找到一种方法跳过它们。在

我试着补充一下:except urllib2.HTTPError:

continue

但我遇到了以下语法错误:

^{pr2}$

以下是我的完整代码:rawdata = []

urllist = []

sharelist = []

mystring = 'http://www.konbini.com'

def print_results(results):

# Print data nicely for the user.

if results:

for row in results.get('rows'):

rawdata.append(row[0])

else:

print 'No results found'

urllist = [mystring + x for x in rawdata]

for row in urllist:

# query the website and return the html to the variable 'page'

page = urllib2.urlopen(row)

except urllib2.HTTPError:

continue

soup = BeautifulSoup(page, 'html.parser')

# Take out the

of name and get its value

name_box = soup.find(attrs={'class': 'nb-shares'})

if name_box is None:

continue

share = name_box.text.strip() # strip() is used to remove starting and trailing

# save the data in tuple

sharelist.append((row,share))

print(sharelist)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值