Python 爬虫——黄磊


# 1
from urllib import request
 
# 构造一个请求
req = request.Request("http://www.baidu.com")
# 然后在打开网页
resp = request.urlopen(req)
# 读取网页内容
print(resp.read())
 
 
 
# 2
#打开网页
reponse=request.urlopen("http://www.baidu.com")
#read方法读取网页内容
print(reponse.read())
 
 
 
 

爬虫程序添加data header
import urllib
from urllib import request
#制定URL
url = "http://www.zhihu.com/signin?next=%2F"
#请求头的部分内容:指定浏览器
user_agent = "Mozilla/4.0 (compatiblc; MSIE 5.5; windows NT)"
#表单的请求参数
values={'username':'27506004','password':'12587496321'}
data=urllib.parse.urlencode(values).encode(encoding='UTF8')
#构建请求头header
headers={'User-Agent':user_agent,
         'Referer':'http://www.zhihu.com/articles'
        }
#构建请求
req=request.Request(url,data=data,headers=headers)
#打开网页
resp=request.urlopen(req)
#读取网页内容
print(resp.read())

爬虫添加cookie
from urllib import  request
from  http import cookiejar
#定义cookie
cookie = cookiejar.CookieJar()
#定义一个cookie处理器,把cookie传进去
handler=request.HTTPCookieProcessor(cookie)
#定义下载器,cookie处理传进去
openner=request.build_opener(handler)
# 下载页面
resp=openner.open("http://www.baidu.com")
# 便利cookie
for item in cookie:
    print('NAME='+item.name)
    print('VALYE'+item.value)
 
 
 
 
# 模拟登录教务系统
from urllib import request
import urllib
from http import cookiejar
 
# 定义文件名
filename = 'cookie.txt'
# 声明MozillaCookieJar对象保存cookie
cookie = cookiejar.MozillaCookieJar(filename)
# 声明一个cookie处理器
handler = request.HTTPCookieProcessor(cookie)
# 定义处理
opener = request.build_opener(handler)
# 定义date:账号+密码
postdata = urllib.parse.urlencode({
    'username': '23567454321',
    'password': '**********'
}).encode(encoding='UTF8')
# 登录教务系统
loginUrl='http://jwc.hnshzy.cn:90/hnshjw/cas/longin.action'
# 模拟登录
result=opener.open(loginUrl,postdata)
# 保存cookie到文件
cookie.save(ignore_discard=True,ignore_expires=True)
 
# 利用保存的cookie请求新网址
new_url='http://jwc.hnshzy.cn:90/hnshjw/cas/longin.action'
# 请求新网页
try:
    result=opener.open(new_url)
except request.HTTPError as e:
    if hasattr(e,'cook'):
        print(e.cook)
except request.URLError as e :
    if hasattr(e,'reason'):
        print(e.reason)
else:
    print(result.read())
正则表达式
import re
 
# 定义正则规则
rexp=re.compile(r"\d{5,11}@\w{2}\.\w{3}")
# 匹配
result=re.match(rexp,'154944@qq.comujdhfbndhdh')
print(result)
 
# 贪婪模式
rexp2=re.compile(r'\w*')
# 匹配
result2=re.match(rexp2,"ssdshdiuwreiuwhr")
print(result2)
 
#边界
rexp3=re.compile(r"^dsd$")
result3=re.match(rexp3,"abcsdsd123")
print(result3)
 
rexp4=re.compile(r"\Aabc")
result4=re.match(rexp4,"abcsdsd")
print(result4)
 
rexp5=re.compile(r"a\b!bc")
result5=re.match(rexp5,"a!bcsdsd")
print(result5)
 
rexp6=re.compile(r"abc|efg")
result6=re.search(rexp6,"aboefgert")
print(result6)
 
rexp7=re.compile(r"(abc){2}")
result7=re.search(rexp7,"abcabcfgebs")
print(result7)
 
rexp8=re.compile(r"(?P<p1>abc)")
result8=re.search(rexp8,"abcefghijk")
print(result8)
 
rexp9=re.compile(r"(\d)abc\1")
result9=re.search(rexp9,"1abc1")
print(result9)
 
rexp10=re.compile(r"(?P<tt>abc)efg(?P=tt)")
result10=re.search(rexp10,"abcefgbc")
print(result10)

 正则表达式语法规则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值