import json import requests import urllib.parse import urllib.request import urllib.parse #拼接前的url url = 'https://www.baidu.com/s?' #拼接数据 data = { 'wd':'周杰伦', 'sex':'男', 'location':'中国台湾省' } #UA伪装 headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' } #使用parse下的urlencode对封装的多个参数编码,后拼接原来的url url = url + urllib.parse.urlencode(data) #请求对象定制,使用request下的Request来定制 request = urllib.request.Request(url=url,headers=headers) #发请求 urlopen response = urllib.request.urlopen(request) #获取响应数据 content = response.read().decode('utf-8') print(content)
Day1 Python爬虫基础 Day1 urllib库的基本使用 百度搜索周杰伦
最新推荐文章于 2024-07-10 09:15:55 发布