python3爬虫requests.get(url)出现http 500错误
2017-11-25 00:03
489 查看
一直想要收集各种一些漏洞的详细描述,但是如果直接去国家信息安全漏洞库,查找,复制,太机械了,宝宝不想这么做,漏洞编号我已经找好了,怎样才能更快的收集它们对应的描述呢?当然是爬虫了,说干就干。
利用requests和beautifulsoup模块进行爬虫。
通过查看网页源码,发现漏洞信息位于class=’d_ldjj’ 的p标签中
将爬到的漏洞描述,按行写入“loudong.xls”excel文件中
请注意,requrl能够正常被打开,但是requests总是出现错误HTTP 500
说明
漏洞编号存放于cn.txt文本中,cn.txt和python位于同一目录中利用requests和beautifulsoup模块进行爬虫。
通过查看网页源码,发现漏洞信息位于class=’d_ldjj’ 的p标签中
将爬到的漏洞描述,按行写入“loudong.xls”excel文件中
代码实现
import pyperclip from xlrd import open_workbook from xlutils.copy import copy url='http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=' #browser=webdriver.Firefox() CNread=open('cn.txt') fw=open("loudong.xls","w") lines=CNread.readlines() k=len(lines) print(k) for i in range(k): requrl=''.join(['http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=',lines[i]]) print(requrl) webbrowser.open(requrl) #requests会自动给连接加上\0A,即十六进制中的换行'\n' res=requests.get(requrl) res.raise_for_status() soup=bs4.BeautifulSoup(res.text) #查找需要的元素 inf=soup.select('.d_ldjj p') li=inf[1].getText() vun=li.split('\n')[1] fw.writelines(vun) print(vun)
请注意,requrl能够正常被打开,但是requests总是出现错误HTTP 500
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-201504-257%0A[/code]
原来requests自动给requrl添加了%0A,导致不能访问,刚开始我还以为是网站为了抵制爬虫而设置的,后面发现,其他的网站也存在这种情况,且requests.get('http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-201504-257')
是正常的,所以‘%0A’并不是被访问的网站加的,而是requets干的,只要我们想办法把它去掉就行,百度一下,%0A发现原来是十六进制的换行,即’\n’,所以需要去掉换行,可将requrl按行分裂,然后取第一部分:res=requests.get(requrl.split('\n')[0])
果然这样修改后,运行成功,看着输出的一个个漏洞信息描述,感觉自己棒棒哒~为自己点赞!
至于为什么会出现这种情况,我也不知道,亲爱的小伙伴,你知道吗?知道的话,解答下呗,你有遇到这种情况吗?
相关文章推荐
- 当编写Servlet时出现type Status report message HTTP method GET is not supported by this URL description The
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- Python requests 多线程抓取 出现HTTPConnectionPool Max retires exceeded异常
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- 出现HTTP Status 405 - HTTP method GET is not supported by this URL 原因及解决方法:
- python-爬虫-requests.get()-响应内容中文乱码
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- 问题:python安装mysql-python 出现Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
- 出现HTTP Status 405 - HTTP method GET is not supported by this URL
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- 【python爬虫】module 'requests' has no attribute 'get'
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- HttpUrlConnectionGet方式请求
- python urllib2 实现HTTP 的GET POST 请求
- HttpUrlConnetionGet
- HttpURLConnection发送Get和Post请求
- HttpUrlConnection用get和post请求发送参数
- python爬虫入门之requests
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片