赞
踩
- from urllib import request
- import re
-
- # 定义url
- page = 100
- url = 'https://tieba.baidu.com/f?kw=%E5%BC%B1%E6%99%BA&ie=utf-8&pn='+str(page)
- try:
- headers ={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36'}
- # 定义请求,传入请求头
- req = request.Request(url,headers=headers)
- # 打开网页
- resp = request.urlopen(req)
- # 打印响应,解码
- # print(resp.read().decode('utf-8'))
- content = resp.read().decode('utf-8')
- print(content)
-
- except request.URLError as e:
- # 打印响应码
- if hasattr(e,'code'):
- print(e.code)
- # 打印异常原因
- if hasattr(e,'reason'):
- print(e.reason)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。