Python爬虫爬取一个网页上的图片地址实例代码,
Python爬虫爬取一个网页上的图片地址实例代码,
本文实例主要是实现爬取一个网页上的图片地址,具体如下。
读取一个网页的源代码:
import urllib.request def getHtml(url): html=urllib.request.urlopen(url).read() return html print(getHtml(http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%E5%A3%81%E7%BA%B8&ct=201326592&lm=-1&v=flip))
利用正则表达式爬取一个网页上的图片地址:
import re import urllib.request def getHtml(url): html=urllib.request.urlopen(url).read() return html def getImg(html): r=r'"thumbURL":"(http://img.+?\.jpg)"' #定义正则 imglist=re.findall(r,html) return imglist html=str(getHtml("http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%E5%A3%81%E7%BA%B8&ct=201326592&lm=-1&v=flip")) print(getImg(html))
运行结果:
总结
以上就是本文关于Python爬虫爬取一个网页上的图片地址实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
相关内容
- Python利用字典将两个通讯录文本合并为一个文本实例,
- Python+tkinter模拟“记住我”自动登录实例代码,pythont
- 详解python使用递归、尾递归、循环三种方式实现斐波那
- Python程序员面试题 你必须提前准备!,python程序员
- python正则中最短匹配实现代码,python最短
- python2 与python3的print区别小结,python2python3
- Python分支结构(switch)操作简介,pythonswitch
- 浅谈Python对内存的使用(深浅拷贝),浅谈python
- 《Python学习手册》学习总结,《python学习手册》
- python+matplotlib绘制3D条形图实例代码,pythonmatplotlib
评论关闭