前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python写网络爬虫

python写网络爬虫

作者头像
py3study
发布2020-01-15 17:48:35
1.1K0
发布2020-01-15 17:48:35
举报
文章被收录于专栏:python3python3

#!/usr/bin/evn python

-- coding: cp936 --

import re #导入正则表达式模块 import urllib #导入urllib模块,读取页面与下载页面需要用到 def getHtml(url): #定义getHtml()函数,用来获取页面源代码 page = urllib.urlopen(url) #urlopen()根据url来获取页面源代码 html = page.read() #从获取的对象中读取内容 return html def getImage(html): #定义getImage()函数,用来获取图片地址并下载 reg = r'src="(.*?.jpg)" width' #定义匹配图片地址的url的正则表达式 imgre = re.compile(reg) #对正则表达式进行编译,运行效率更高 imagelist = imgre.findall(html) #使用findall()查找html中匹配正则表达式的图片url x = 0 for imageurl in imagelist: urllib.urlretrieve(imageurl,'picture_%s.jpg' % x) #urlretrieve()下载文件 x +=1 uri = raw_input("请输入网址: ") r = r'^http://' if re.match(r,uri): html2 = getHtml(uri) else: html2 = getHtml("http://" + uri) getImage(html2) 运行脚本test.py

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • -- coding: cp936 --
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档