AI摘要:本文介绍了使用Python进行爬虫的基础知识,包括Cookie模拟登录和使用POST请求获取金山翻译的方法。通过模拟登录,可以在爬取网页时绕过登录验证,获取到需要的数据。而使用POST请求可以向服务器发送数据,并获取服务器返回的结果。文章中给出了具体的代码示例,帮助读者理解和实践这些方法。
一、Cookie 模拟登录
import requests
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'Cookie':''
}
url = ''
res = requests.get(url, headers = headers)
print(res.text)
二、使用post请求获取金山翻译
import requests
import json
import random
from fake_useragent import UserAgent
# 获取请求url
url = 'https://ifanyi.iciba.com/index.php?c=trans'
# 构造headers
headers = {
'User-Agengt':UserAgent().random
}
#输入待译文本
word = input("请输入待译文本:")
# 构建data参数字典
post_data ={
'from':'zh',
'to':'en',
'q': word
}
#发起请求
res = requests.post(url, headers = headers, data = post_data)
#获取请求内容
# print(res.text)
#将Json数据解析为字典
dic = json.loads(res.text)
#获取输出内容格式
# print(type(dic))
#打印译文
print(dic["out"])
请输入待译文本: 金山翻译
Jinshan translation
import requests
import json
# import random
# from fake_useragent import UserAgent
# 获取请求url
url = 'https://app.languagex.com/api/translator/translation/text'
# 构造headers
headers = {
'User-Agengt':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'Cookie': 'Hm_lvt_d78dae91eafe9d7af5ea9e954247f348=1704525651; token=eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjE1NzI5NjUwNjIzMDU0OTcwOTAsIm1pZCI6MTY4MjIzMDI2MTM5NzAwMSwidXNlcm5hbWUiOiJMWDYzMmM3OTdkYTdmNjBhYzJlMjM5YjMyOCIsImV4cCI6MTcwNTU5MjUzNH0.YktHsPJ7aXhERNmvLQ3w0SAwabweGsoqsQ1wFAbiFoQ; org=eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTcwMTg1MTc3NzAwNjg3MywibmFtZSI6IumrmOagoee_u-ivkemhueebriIsInR5cGUiOiJFTlRFUlBSSVNFIiwicm9sZUlkIjoyLCJ0ZWFtSWRMaXN0IjpbMTcwMTg1MTc3NzAwNjg3N119.nCdkNCDrkBxHCDJxvYGZo4nFOUgsGKwUPDq4KwaStTw; Hm_lpvt_d78dae91eafe9d7af5ea9e954247f348=1705333336'
}
#输入待译文本
# word = input("请输入待译文本:")
# 构建data参数字典
post_data ={
"domain":"general",
"engine":"chatgpt",
"engineKey":"chatgpt",
"sourceLang":"zh",
"sourceText":"美国",
"targetLang":"en",
"terms":[]
}
#发起请求
res = requests.post(url, headers = headers, data = post_data)
#获取请求内容
# print(res.text)
print(res)
#将Json数据解析为字典
# dic = json.loads(res.text)
#获取输出内容格式
# print(type(dic))
#打印译文
# print(dic["out"])
<Response [415]>
评论 (0)