zoukankan      html  css  js  c++  java
  • Python爬虫基础

    1、urllib和urllib2的区别:

    1、获取baidu的网页源代码

    urllib是python内置的http请求库。

    urllib.request:请求模块。

    1 import urllib.request
    2 
    3 resp = urllib.request.urlopen('http://www.baidu.com')
    4 # resp:<http.client.HTTPResponse object at 0x029971D0>
    5 # resp是一个HTTPResponse对象
    6 # resp.read()读取urlopen(url)中url里面的内容
    7 print(resp.read().decode('utf-8'))

     python 正则表达式:re.S

    re.S 将字符串作为一个整体,将" "当作一个普通的字符加入到这个字符串中,在整体中进行匹配。

    import re
    a = '''asdfsafhellopass:
        234455
        worldafdsf
        '''
    # '.'是匹配除"
    "以外的任何字符,也就是说,它是在一行中进行匹配。
    b = re.findall('hello(.*?)world', a)
    # re.S 将字符串作为一个整体,将"
    "当作一个普通的字符加入到这个字符串中,在整体中进行匹配
    c = re.findall('hello(.*?)world', a, re.S)
    print('b is', b)
    print('c is', c)

    输出结果:

    b is []
    c is ['pass: 234455 ']

  • 相关阅读:
    python实现从生成器中取固定角标的元素
    python-高阶函数
    git学习手册
    python批量进行文件修改操作
    python第一站
    LoadRunner,一个简单的例子
    python3中urllib2的问题
    gitlab 随笔
    sed 删除文本
    sed 修改文本
  • 原文地址:https://www.cnblogs.com/niuwa/p/10260523.html
Copyright © 2011-2022 走看看