python爬虫
一、python模块 1、urllib.request常规使用 import urllib.request #1、指定URL url = 'http://www.baidu.com' #2、发起请求:urlopen可以根据指定的url发起请求,且返回一个响应对象 response = urllib.request.urlopen(url=url) #3、获取页面数据:read函数返回的就是响应对象中存储的页面数据(byte) response_text = response.read() #4、持久化存储 with open('./baidu.html','wb') as f: f.write(response_text) print('写入成功') urllib.request urllib.request处理url中文 import urllib.request import urllib.parse #1、指定URL # url = 'https://tieba.baidu.com/f?ie=utf-8&kw=你好&fr=search' url = 'https://tieba.baidu.com/f?ie=utf-8&kw=%s&fr=search' #url不可以存在非ascii编码的字符数据 wd = urllib.parse.quote("你好") new