1 import urllib.request 2 import urllib.parse 3 4 url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=' 5 6 # 请求对象的定制为了解决反爬的第一种手段 7 headers = { 8 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.41' 9 } 10 11 # 要将周杰伦三个字变成unicode编码格式 12 name = urllib.parse.quote('周杰伦') 13 print(name) # %E5%91%A8%E6%9D%B0%E4%BC%A6 14 url = url + name # 将url拼接一下 15 16 17 # 因为urlopen不接受字典形式的headers,所以要定制对象。 18 # 请求对象定制 19 request = urllib.request.Request(url=url, headers=headers) 20 21 # 模拟浏览器向服务器发送请求 22 response = urllib.request.urlopen(url) 23 24 # 获取响应的数据 25 content = response.read().decode('utf-8') 26 27 # 打印数据 28 print(content)
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/268060.html