为了练手,使用爬虫爬一个“你懂得”图床的,使用的是urlretrieve函数,不但速度慢,还总是会报错,不是open的timeout就是上面提到的socket error。在网上找了许多办法诸如在urllib2.Request.urlopen().read()后需要调用close()关闭等方法并未奏效。
由于不想麻烦scrapy等库,所以发现了个简单粗暴的办法:
直接使用urllib自带的ope...
分类:
编程语言 时间:
2015-04-26 13:54:36
阅读次数:
295
前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍。当然 urllib 还有一些其它很有用的辅助方法,比如对 url 进行编码、解码等等。辅助方法:1. urllib.quote(string[,safe]) : 对字符串进行编码,参数sa...
分类:
编程语言 时间:
2015-04-24 16:11:11
阅读次数:
137
说明:仅为测试下载图片、正则表达式
测试url为钢铁侠贴吧的一个介绍mark各代盔甲帖子
以下代码将第一页的图片全部下载到本程序根目录#!/usr/bin/env python
#! -*- coding: utf-8 -*-
import urllib,urllib2
import re
#返回网页源代码
def getHtml(url):
html = urllib2.urlopen...
分类:
编程语言 时间:
2015-04-23 13:27:53
阅读次数:
184
可以把urllib2当作urllib的扩增,比较明显的优势是urllib2.urlopen可以接受Request对象作为参数,从而可以控制HTTP Request的headers,进而实现模拟浏览器、模拟登录等操作。
做HTTP Request时应当尽量使用urllib2库,但是urllib.urlretrieve函数以及urllib.quote等一系列quote和unquote功能没有被加入ur...
分类:
编程语言 时间:
2015-04-23 13:27:14
阅读次数:
128
案例讲解import urllib #调用uerllib
import webbrowser
url = 'http://blog.csdn.net/xlgen157387'
content = urllib.urlopen(url).read()
open('test.html','w').write(content) #写入到test.html文件中
webbrowser.open_new_...
分类:
编程语言 时间:
2015-04-18 16:11:40
阅读次数:
175
python 爬虫爬取美女图片
#coding=utf-8
import urllib
import re
import os
import time
import threading
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg...
分类:
编程语言 时间:
2015-04-11 09:02:27
阅读次数:
226
1 #!usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import urllib 4 import re 5 6 def getHtml(url): 7 page = urllib.urlopen(url) 8 html = page....
分类:
Web程序 时间:
2015-04-07 13:25:57
阅读次数:
145
判断字符串编码使用 chardet 可以很方便的实现字符串/文件的编码检测。尤其是中文网页,有的页面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些页面,知道网页编码很重要>>> import urllib>>> html = urllib.urlopen('http://www.ch...
分类:
编程语言 时间:
2015-04-04 14:58:33
阅读次数:
234
importurllib.request,os;pf=‘PackageControl.sublime-package‘;ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),‘wb‘).write(urllib.request.urlopen(‘http://..
分类:
其他好文 时间:
2015-03-21 21:27:34
阅读次数:
201
# -*- coding: UTF-8 -*-import urllibrawdata=urllib.urlopen("http://open.lewei50.com/").read()print rawdata # -*- coding: UTF-8 -*-import urllibrawdat....
分类:
其他好文 时间:
2015-03-17 01:59:54
阅读次数:
132