爬虫

需要的步骤

1.使用python发送服务器请求
2.请求响应之后,会得到相应对象response(源码,以及其他的响应信息),response.read( )-------源码信息-----字节
如果需要的是网页的源码信息
字节.decode() 转换成字符串,默认按照utf-8编码,将字节转换成字符串
3.使用抓取工具(正则表达式)来对字符串信息进行处理
4.保存抓取

一、使用python发送服务器请求
from urllib.request import urlopen
import re
#urlopen(url)向服务器发送请求,返回值是一个响应对象response
url="http://lagou.com"
response=urlopen(url)
二、获取源码信息

read()获得字节,decode()将字节转换成字符串,默认utf-8

#print(response.read().decode())
html_text=response.read().decode()
三、使用正则表达式来获取信息
res_url="<a.*?href=\"(http.*?)\".*?>"
r=re.findall(res_url,html_text,re.M|re.S|re.I)
# print(r)
for i in r:
	print(i)
四、信息的存储

excel,json,数据库

with open("c:/lagou.csv","wt",newline="") as f:
	w=csv.writer(f)
	for i in r:
		#print
		w.writerow([i])

文字信息
图片信息
获取图片信息

img_url="https://www.baidu.com/img/bd_logo1.png?where=super"
response_img=urlopen(img_url)
#print(response_img.read())
with open("1.jpg","wb") as f:
	f.write(response_img.read())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值