Python爬虫爬取一个网页上的图片地址实例代码

yipeiwu_com6年前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 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程

实例如下所示: import requests import re,sys,os import json import threading import pprint class s...

Python 微信爬虫完整实例【单线程与多线程】

本文实例讲述了Python 实现的微信爬虫。分享给大家供大家参考,具体如下: 单线程版: import urllib.request import urllib.parse impo...

python 中xpath爬虫实例详解

python 中xpath爬虫实例详解

案例一: 某套图网站,套图以封面形式展现在页面,需要依次点击套图,点击广告盘链接,最后到达百度网盘展示页面。 这一过程通过爬虫来实现,收集百度网盘地址和提取码,采用xpath爬虫技术...

Python爬虫天气预报实例详解(小白入门)

Python爬虫天气预报实例详解(小白入门)

本文研究的主要是Python爬虫天气预报的相关内容,具体介绍如下。 这次要爬的站点是这个:http://www.weather.com.cn/forecast/ 要求是把你所在城市过去...

python爬虫超时的处理的实例

如下所示: #coding:utf-8 ''''' Created on 2014-7-24 @author: Administrator ''' import url...