zoukankan      html  css  js  c++  java
  • python 爬爬爬 基本函数~

    https://docs.python.org/2/howto/urllib2.html#data

    http://zhuoqiang.me/python-urllib2-usage.html

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 import os
     5 import urllib
     6 import urllib2
     7 import re
     8 import cookielib
     9 
    10 httpHandler = urllib2.HTTPHandler(debuglevel=1)
    11 httpsHandler = urllib2.HTTPSHandler(debuglevel=1)
    12 opener = urllib2.build_opener(httpHandler, httpsHandler)
    13 urllib2.install_opener(opener)
    14 
    15 
    16 headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'}
    17 values = {
    18     #'username':'xxx',
    19     #'password':'xxx',
    20 }
    21 postdata = urllib.urlencode(values)            # 表单
    22 
    23 # GET
    24 req = urllib2.Request(url='https://www.baidu.com/', data=None, headers=headers)
    25 response = urllib2.urlopen(req)
    26 html = response.read()
    27 response.close()
    28 print html
    29 
    30 # 图片等二进制数据
    31 req = urllib2.Request(url='https://www.baidu.com/img/bd_logo1.png', data=postdata, headers=headers)
    32 operate = opener.open(req)
    33 data = operate.read()
    34 operate.close()
    35 
    36 f = open('baidu.png', 'wb')
    37 f.write(data)
    38 f.flush()
    39 f.close()
  • 相关阅读:
    使用c#读取/解析二维码
    MVVM中的RelayCommand与AsyncCommand
    MVVM模式下的OpenFileDialog
    集成Source Monitor至VS中
    [转]异步command的实现
    使用Messenger实现MVVM的对话框弹出
    使用NPOI访问、控制Excel
    win11更新
    Codeforces Round #749 总结
    Codeforces Round #697 (Div. 3)
  • 原文地址:https://www.cnblogs.com/hangj/p/4680525.html
Copyright © 2011-2022 走看看