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 将字符串作为一个整体,将"\n"当作一个普通的字符加入到这个字符串中,在整体中进行匹配。

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

输出结果:

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

 

posted @ 2019-01-12 18:00  云帆济海2017  阅读(170)  评论(0编辑  收藏  举报