python的urllib2和urllib爬虫及区别
来源:互联网 发布:小蜜蜂软件官方网 编辑:程序博客网 时间:2024/06/05 14:19
urllib和urllib2的区别,urllib只能根据url爬取,不能设置头信息,但可以对url进行编码;urllib2可以设置request请求头信息
# -*- coding: utf-8 -*-import urllibf = urllib.urlopen('http://www.cnblogs.com/')result = f.read() #读取html页面的第一行print result
# -*- coding: utf-8 -*-import urllib2import sysdef clawer(url): send_headers={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection':'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0' } req = urllib2.Request(url,headers=send_headers) r = urllib2.urlopen(req) html = r.read() #返回网页内容 receive_header = r.info() #返回的报头信息 print sys.getfilesystemencoding() #html = html.decode('utf-8','replace').encode(sys.getfilesystemencoding()) #转码:避免输出出现乱码 #print receive_header print html.decode('gbk')url= "http://www.jb51.net"url = "http://uvs.youdao.com/search?site=photogalaxy.163.com&sort=time&channelid=&q=%B7%C9%BB%FA&length=10&start=0" #网页地址clawer(url)
import urllibimport urllib2values = {"username":"1016903103@qq.com","password":"XXXX"}data = urllib.urlencode(values)url = "https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"request = urllib2.Request(url,data)response = urllib2.urlopen(request)print response.read()
0 0
- python的urllib2和urllib爬虫及区别
- python爬虫--urllib2和urllib区别
- python的httplib、urllib和urllib2的区别及用
- python的httplib、urllib和urllib2的区别及用
- python的httplib、urllib和urllib2的区别及用
- python的urllib和urllib2的区别
- python urllib2和urllib的区别
- Python:urllib 和urllib2之间的区别
- Python:urllib 和urllib2之间的区别
- Python:urllib 和urllib2之间的区别
- python urllib 和urllib2的区别
- Python:urllib 和urllib2之间的区别
- python urllib 和urllib2的区别
- Python:urllib 和urllib2之间的区别
- python中urllib和urllib2的区别!
- python的urllib和urllib2
- 从0开始学python:urllib和urllib2的区别
- Python模块之urllib和urllib2之间的区别
- C# for Unity快速入门-C#语言入门(B)
- Android studio怎么直接对apk签名?
- 线程、进程
- Combination Sum II
- 什么是脏读、不可重复读、幻读
- python的urllib2和urllib爬虫及区别
- 实习杂记(30):eclipse小知识点积累
- Java编程风格节选
- python matplotlib入门
- 根据wsdl生成客户端:Use of SOAP Encoding is not supported
- C/C++_
- iOS面试系列5
- HDU 3032 Nim or not Nim? && HDU 5795 A Simple Nim (Lasker's Nim游戏、SG函数、取走-分割游戏)
- MAVEN项目的配置文件pom.xml元素的含义