zoukankan      html  css  js  c++  java
  • 学习python的第七天笔记

    37、055
    修改headers
    ①通过Request的headers参数修改
    ②通过Request.add_header()方法添加

    代理
    1、创建一个proxy_support = urllib.request.ProxyHandler({})

    2、定制、创建一个opener
    opener = urllib.request.builb_opener(proxy_support)

    3、安装opener
    urllib.request.install_opener(opener)

    4、调用opener
    opener.open(url)

    例如:
    import urllib.request
    import random

    url = 'http://www.whatismyip.com.tw'#可以查看自己IP地址的网站,也可以是其他的网。
    iplist = ['1.245.107.123:3128','203.246.112.133:3128','200.89.174.245:8080']
    proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')

    print(html)

    38、055
    隐藏
    import urllib.request
    import urllib.parse
    import json

    content = input("请输入要翻译的内容:")

    url = 'http://fy.iciba.com/ajax.php?a=fy'
    data={}
    data['f'] = 'auto'
    data['t'] = 'auto'
    data['w'] = content
    data = urllib.parse.urlencode(data).encode('utf-8')

    req = urllib.request.Request(url,data)
    #模拟网页登录
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36')

    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')

    targel = json.loads(html)
    print("翻译结果:%s"%targel['content']['out'])

    39、56

    40、057
    正则表达式需要导入re模块,例如import re
    r.search(r'需要找的内容','有内容的语句')
    r.search(r'[查找内容]','查找语句')中括号可以创建个字符类,只要匹配到中括号内任何一个字母或数字都算匹配到了
    r.search(r'ab{3,10}c','abbbbbc')大括号中是允许b的次数为三到十次

    41、058
    ^意思是必须是开头。
    $意思是以什么结尾。例如:r.search(r'abc$',123.abc)这就可以匹配到
    *={0,}从零到正无穷
    +={1,}从一到正无穷
    ?={0,1}从零到一
    #re.findall()可以将匹配到的内容打包成一个表。

    42、059
    p = re.compile("需要调用的内容")
    p.search("查询语句")
    p.findall("查询语句")
    re.VERBOSE模式下支持空格等

  • 相关阅读:
    SpringMVC详解
    会话控制cookie和session
    jsp详解
    Jquery Enter事件
    JS获取request字符串
    net 2.0使用ajax
    C#通过WebClient/HttpWebRequest实现http的post/get方法
    WCF Rest:不使用UriTemplate使用post方式传参解决HTTP400问题以及参数映射问题
    关于ASP.NET 中站点地图sitemap 的使用
    Web.config自定义节点configSections
  • 原文地址:https://www.cnblogs.com/dcpb/p/11595070.html
Copyright © 2011-2022 走看看