Python检查多个url能否正常运行

拖拉了2个月,个人项目终于开始动工,也在不断的学习和成长。手里拿到了一份OpenSources(github上直接搜索可以查到),感兴趣的朋友也可以下载csv文件进行练手。
import pandas as pd
import urllib.request
import time
#这里读取文件所在的位置
data =pd.read_csv('/Users/Macbook/Documents/GitHub/opensources/sources/sources.csv')
data[:30]#看一下前30行的数据是什么样的
#把pd中的一列写入fakenews.txt中
data['Unnamed: 0'].to_csv('fakenews.txt', sep='\t', index=False)


opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/49.0.2')]
file = open('fakenews.txt')
lines = file.readlines()
checklist=[]
for line in lines:
    #这里注意一下:适用于爬虫和检测url有效性
    htp='http://'
    line = htp + line
    temp=line.replace('\n','')
    checklist.append(temp)
print(checklist)

print('测试:')
for a in checklist:
    tempUrl = a
    try :
        opener.open(tempUrl)
        print(tempUrl+'没问题')
        time.sleep(2)
    except urllib.error.URLError:
        print(tempUrl+'访问页面出错')
        time.sleep(2)
    # try except语句可以很好地帮助我们跳过错误,继续运行代码
    except Exception as e:
        print(a,e)
    time.sleep(1)
    #time模块能够避免被当作机器

Anyway Practice makes perfect
有任何问题可以相互交流,这里也要感谢@程序猿全敏提供的思路支持,本篇经验贴对其中经常报误的地方进行了修改和补充~

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值