案例一:
- # 使用 urllib
- import urllib.request
- # 使用 json
- import json
-
- # 定义 header
- headers = {
- # 'Accept': '*/*',
-
- # 因为有 `Accept-Encoding` 这行会报错:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
- # 'Accept-Encoding': 'gzip, deflate, br', # 在写爬虫的时候这一句话一定要注释掉,因为接受编码格式没有 `utf-8`
-
- # 'Accept-Language': 'zh-CN,zh;q=0.9',
- # 'Acs-Token': '1668323009647_1668329342293_ts2LNTmAMNu8bz6UcQlmajMjkFeD7EM7TNicuMqP14qLPx6R8FOs/pHR9jGpORyVc3jkLtMD7dh7/9XDK7+b5SqO0f2bzrYB1DzNkaXGMwkcPyDo9cygYroE1pFoncx4VbCQu/ieJyUK+TbYU4atAB/XCth4Lc8zNYRCQuOtbLb2zJ9RX1Jj7krqZIfNr14muZP1N5+rq7cjPbnBgvf5V6m03XLliDp4ueln/xS/qSQVL3XiWSOpemYY9gD9UHaUVPGIi4Q6uIT/05yCxxhd4kJCfeRys/AiOLQ+nbNaXUGoyTBZg90HLn6A9NTb5HAF',
- # 'Connection': 'keep-alive',
- # 'Content-Length': '135',
- # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
-
- # 防爬方式的一种,验证 `Cookie`
- 'Cookie': '换成自己百度账号的 Cookie',
-
- # 'Host': 'fanyi.baidu.com',
- # 'Origin': 'https://fanyi.baidu.com',
- # 'Referer': 'https://fanyi.baidu.com/',
- # 'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
- # 'sec-ch-ua-mobile': '?0',
- # 'sec-ch-ua-platform': '"macOS"',
- # 'Sec-Fetch-Dest': 'empty',
- # 'Sec-Fetch-Mode': 'cors',
- # 'Sec-Fetch-Site': 'same-origin',
- # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
- # 'X-Requested-With': 'XMLHttpRequest'
- }
-
- # 请求地址
- url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
-
-
- # 参数
- params = {
- 'from': 'zh',
- 'to': 'en',
- 'query': '名称',
- 'transtype': 'realtime',
- 'simple_means_flag': '3',
- 'sign': '386125.67452',
- 'token': '换成自己百度账号的 token',
- 'domain': 'common'
- }
-
- # 上面直接使用参数字符串会报错,是因为 post 请求参数必须要要进行编码
- data = urllib.parse.urlencode(params).encode('utf-8')
-
- # 模拟浏览器向服务器发送请求
- request = urllib.request.Request(url=url, data=data, headers=headers)
-
- # 模拟浏览器向服务器发送请求
- response = urllib.request.urlopen(request)
-
- # 获取内容字符串
- content = response.read().decode('utf-8')
-
- # 将字符串转成 json
- obj = json.loads(content)
-
- # 输出 json
- print(obj)
-
案例二:
- # 测试 https://weibo.cn/pub/,抓取个人详细资料:https://weibo.cn/xxxx/info
- # 重点:
- # 直接访问个人资料页,页面编码格式是 UTF-8 但是还是报编码格式错误,
- # 是因为没有进入到个人信息页面,而是跳转到了登录页面,登录页面不是 UTF-8 所以报错,也就是校验信息没带全,算没登录
-
- # 引用 urllib
- import urllib.request
-
- # 请求头
- headers = {
- # 校验 ua ,防爬方式的一种
- 'user-agent': '',
- # 主要就是少了这一行,cookie 携带着登录信息,如果有登录之后的 cookie,那么就能通过携带 cookie 进入任何页面,防爬方式的一种
- 'cookie': '',
- # 用于判断当前路径是不是由上一个路径进来的,如果不是也会报错,一般情况是做图片的防盗链,防爬方式的一种,这种方式属于可选防爬方式不是每个网站都校验这个
- 'referer': 'https://weibo.cn/'
- }
-
- # 请求地址
- url = 'https://weibo.cn/xxx/info'
-
- # 请求对象
- request = urllib.request.Request(url=url, headers=headers)
-
- # 模拟浏览器向服务器发起请求
- response = urllib.request.urlopen(request)
-
- # 如果报错编码格式错误,可以通过输出 read() 内容查看网页编码,但是只适用个别网页
- # print(response.read()) # <meta charset="gb2312">
-
- # 获取响应数据
- content = response.read().decode('utf-8')
- # print(content)
-
- # 将页面数据保存,方便确认当前在哪(打开保存的文件会发现是 登录页面)
- with open('weibo.html', 'w', encoding='utf-8') as f:
- f.write(content)
-