python下载urllib3_使用urllib3下载网页
我正在为一个作业编写一个程序,它使用urllib3下载一个网页并将其存储在词典中。(我用的是spyder 3.6)
程序给了我一个“AttributeError”,我不知道我做错了什么。这是我的代码和我为作业编写的一步一步的注释。在#Downloading a webpage
import urllib3
import sys
#these import statements allow us to use 'modules' aka 'libraries' ....
#code written by others that we can use
urlToRead = 'http://www.google.com'
#This value won't actually get used, because of the way the while loop
#below is set up. But while loops often need a dummy value like this to
#work right the first time
crawledWebLinks = {}
#Initialize an empty dictionary, in which (key, value) pairs will correspond to (short, url) eg
#("Goolge" , "http://www.google.com")
#Ok, there is a while loop coming up
#Here ends the set up
while urlToRead != ' ':
#This is a condition that dictates that the while loop will keep checking
#as long as this condition is true the loop will continue, if false it will stop
try:
urlToRead = input("Please enter the next URL to crawl")
#the "try" prevents the program from crashing if there is an error
#if there is an error the program will be sent to the except block
if urlToRead == '':
print ("OK, exiting loop")
break
#if the user leaves the input blank it will break out of the loop
shortName = input("Please enter a short name for the URL " + urlToRead)
webFile = urllib3.urlopen(urlToRead).read()
#This line above uses a ready a readymade function in the urllib3 module to
#do something super - cool:
#IT takes a url, goes to the website for the url, downloads the
#contents (which are in the form of HTML) and returns them to be
#stored in a string variable (here called webFile)
crawledWebLinks[shortName] = webFile
#this line above place a key value pair (shortname, HTML for that url)
#in the dictionary
except:
#this bit of code - the indented lines following 'except:' will be
#excecuted if the code in the try block (the indented following lines
#the 'try:' above) throw and error
#this is an example of something known as exeption-handling
print ("*************\nUnexpected Error*****", sys.exc_info()[0])
#The snip 'sys.exc_info()[0]' return information about the last
#error that occurred -
#this code is made available through the sys library that we imported above
#Quite Magical :)
stopOrProceed = input("Hmm..stop or proceed? Enter 1 to stop, enter anything else to continue")
if stopOrProceed ==1 :
print ('OK...Stopping\n')
break
#this break will break out of the nearest loop - in this case,
#the while loop
else:
print ("Cool! Let's continue\n")
continue
# this continue will skip out of the current iteration of this
#loop and move to the next i.e. the loop will reset to the start
print (crawledWebLinks.keys())
python下载urllib3_使用urllib3下载网页相关推荐
- Python利用bs4批量抓取网页图片并下载保存至本地
Python利用bs4批量抓取网页图片并下载保存至本地 使用bs4抓取网页图片,bs4解析比较简单,需要预先了解一些html知识,bs4的逻辑简单,编写难度较低.本例以抓取某壁纸网站中的壁纸为例.(b ...
- python批量下载静态页面_Python静态网页爬取:批量获取高清壁纸
前言 在设计爬虫项目的时候,首先要在脑内明确人工浏览页面获得图片时的步骤 一般地,我们去网上批量打开壁纸的时候一般操作如下: 1.打开壁纸网页 2.单击壁纸图(打开指定壁纸的页面) 3.选择分辨率(我 ...
- 使用Python脚本自己实现数据下载,还有花哨的下载进度显示
这篇博客简单介绍python下载文件,并加入花哨的下载进度显示方法,涉及Python文件操作的技巧,和一些函数库的使用. 环境 Python 3.6 requests模块 对应文件的下载链接 (是不是 ...
- python实现http下载文件-Python HTTP下载文件并显示下载进度条功能的实现
下面的Python脚本中利用request下载文件并写入到文件系统,利用progressbar模块显示下载进度条. 其中利用request模块下载文件可以直接下载,不需要使用open方法,例如: im ...
- 2021-03-10 Python多线程爬虫快速批量下载图片
Python多线程爬虫快速批量下载图片 1.完成这个需要导入的模块 urllib,random,queue(队列),threading,time,os,json 第三方模块的安装 键盘win+R,输入 ...
- Python 3 爬虫之批量下载字帖图片
朋友想下载这62个网页中的字帖图片:http://www.yac8.com/news/11003.html 一.要点 1. Chrome 「审查元素」中看到的源代码与真实的源码不同. 2. 图片网址附 ...
- python实现酷狗音乐下载,以及利用tk界面可视化
思路分析:从酷狗主页http://www.kugou.com/,搜索框输入搜索歌曲后,发现请求网页变成 http://www.kugou.com/yy/html/search.html#searchT ...
- python音乐下载器交互界面设计_用python实现的百度音乐下载器-python-pyqt-改进版...
之前写过一个用python实现的百度新歌榜.热歌榜下载器的博文,实现了百度新歌.热门歌曲的爬取与下载.但那个采用的是单线程,网络状况一般的情况下,扫描前100首歌的时间大概得到40来秒.而且用Pyqt ...
- 使用python编写的落网电台下载工具
使用python编写的落网电台下载工具 落网是个非常不错的独立音乐介绍网站,但是上面提供的歌曲仅供试听,没有提供下载链接.相信不少同学都想过有没工具把歌曲下载下来呢?答案是:有. 我最早是使用遨游浏览 ...
- python爬虫之壁纸的下载
python爬虫之壁纸的下载 好久没有写博客了,暑假这段时间在学校里面忙着写小论文,没机会写我最喜欢的爬虫了(虽然很简单,但架不住哥喜欢...) 好了,废话不多说了,开始把 本次爬虫爬取的是 手机壁纸 ...
最新文章
- Python入门知识
- 在SolidWorks 3D CAD中构建乐高直升机
- windows下vs2013使用C++访问redis
- 机器学习:贝叶斯分类器,朴素贝叶斯,拉普拉斯平滑
- jieba分词_Jieba.el – 在Emacs中使用jieba中文分词
- poj 1180 斜率优化dp
- oracle dba角色_DBA在“即服务”世界中角色的不断变化
- 修改MYSQL密码及权限
- django调用python脚本返回_讲解django rest framework 自定义返回方法
- Github官方app分析——用户身份验证模块
- [转载] C++ std::vector指定位置插入
- Mac系统下设置Maven环境
- R语言绘制流程图(一)
- 百度黑洞效果实现原理
- Unity(一)Scene和GameObject
- 搞模具的想转行学计算机,为什么很少人去做模具设计学徒,看看这里你就明白了!...
- Leetcode分类练习-查找(2)对撞指针
- 书评:《软件创新之路-冲破高技术营造的牢笼》
- 未来展望 计算机,计算机的未来展望(The future of computers).doc
- 欧尼酱讲JVM(12)——本地方法接口