爬虫1 认识和基本爬取UA伪装

robots.txt 协议

君子协议

如果爬取了robots.txt 不允许被爬的东西,会封你IP,你还可以进局子。

可以在网站根目录下加上访问看下存在不,大网站一般是存在的。

百度翻译[练练手 UA 伪标]

# 作者:咸瑜
# 仅供学习使用!!!
import json

import requests

URL = "https://fanyi.baidu.com/sug"

# header UA 伪装
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.47'}

key = input("输入要翻译的单词:")
# 参数
data = {
    "kw": key
}

response = requests.post(url=URL, data=data, headers=header).json()

for item in response:
    if type(response[item]) == list:
        for listItem in response[item]:
            print('单词:', listItem['k'], '    中文翻译:', listItem['v'])

image-20221222193806216

posted @ 2022-12-22 19:39  咸瑜  阅读(42)  评论(0编辑  收藏  举报