博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python爬虫之基本知识
阅读量:6881 次
发布时间:2019-06-27

本文共 2267 字,大约阅读时间需要 7 分钟。

一、请求-响应

在利用python语言实现爬虫时,主要用到了urllib和urllib2两个库。首先用一段代码说明如下:

1 import urllib2 import urllib23 4 url="http://www.baidu.com"5 request=urllib2.Request(url)6 response=urllib2.urlopen(request)7 print response.read()

我们知道一个网页就是以html为骨架,js为肌肉,css为衣服所构成的。上述代码所实现的功能就是把百度网页的源码爬取到本地。

其中,url为要爬取的网页的网址;request发出请求,response是接受请求后给出的响应。最后用read()函数输出的就是百度网页的源码。

二、GET-POST

两者都是向网页传递数据,最重要的区别是GET方式是直接以链接形式访问,链接中包含了所有的参数,当然如果包含了密码的话是一种不安全的选择,不过你可以直观地看到自己提交了什么内容。

POST则不会在网址上显示所有的参数,不过如果你想直接查看提交了什么就不太方便了,大家可以酌情选择。

POST方式:

1 import urllib2 import urllib23 values={
'username':'2680559065@qq.com','Password':'XXXX'}4 data=urllib.urlencode(values)5 url='https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'6 request=urllib2.Request(url,data)7 response=urllib2.urlopen(request)8 print response.read()

GET方式:

import urllibimport urllib2values={
'username':'2680559065@qq.com','Password':'XXXX'}data=urllib.urlencode(values)url = "http://passport.csdn.net/account/login"geturl = url + "?"+datarequest=urllib2.Request(geturl)response=urllib2.urlopen(request)print response.read()

三、异常处理

处理异常时,用到了try-except语句。

1 import urllib22 3 try:4     response=urllib2.urlopen("http://www.xxx.com")5 except urllib2.URLError,e:6     print e.reason

通过上述的介绍及代码展示,我们已经初步认识了爬虫过程,希望对大家有所帮助。

爬取图片:

import urllib
import urllib.request
import re
import os
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def gethtml(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    req = urllib.request.Request(url=url, headers=headers)
    page=urllib.request.urlopen(req)
    html=page.read()
    html=html.decode('utf-8')
    return html
    
def getImg(html):
    reg='<img src="(http.*?\.png)"'
    imgre=re.compile(reg)
    imglist=imgre.findall(html)
    return imglist
    
def make_dir(floder):
    path=os.getcwd()+'/'+floder
    if not os.path.isdir(path):
        os.makedirs(path)
    return path
def save_image(path,imglist):
    x=0
    for imgurl in imglist:
        print(imgurl)
        urllib.request.urlretrieve(imgurl,'{}/{}.png'.format(path,x))
        x=x+1
    
if __name__=='__main__':
    html=gethtml('https://blog.csdn.net/yql_617540298/article/details/81411995')
    imglist=getImg(html)
    path=make_dir('test')
    save_image(path,imglist)
 

转载于:https://www.cnblogs.com/zhenpengwang/p/8505860.html

你可能感兴趣的文章
ORACLE 学习笔记1
查看>>
vmware格式转换
查看>>
beego mysql in查询
查看>>
git 回退版本
查看>>
go mod 在使用私有gitlab时“go-get=1”错误解决
查看>>
Tableau Server 9.1.2 配置集群手册
查看>>
java逻辑运算符
查看>>
org.bson.codecs.configuration.CodecConfigurationException
查看>>
jsoup抓取网页+详细讲解
查看>>
Python实现修改Windows CMD命令行输出颜色(完全解析)
查看>>
HQL语句讲解
查看>>
服务器安全狗linux版 V2.4 发布 增加网页木马扫描
查看>>
安全狗服云web端V3.4(企业服务)版上线
查看>>
在Android Library的Module中按渠道依赖
查看>>
对javascript匿名函数的理解(透彻版)
查看>>
使用virtualbox安装centos6的内置无线网卡桥接设置
查看>>
java调用http接口(HttpURLConnection的使用)
查看>>
java代码内,获得jsp产生的html
查看>>
jquery.validate remote 和 自定义验证方法
查看>>
hibernate使用sql查询
查看>>