一、打开网页获取页面源码
from selenium.webdriver.chrome import webdriver
driver = webdriver.WebDriver(executable_path="chromedriver.exe")
url='https://www.xinpianchang.com/channel/index/sort-like?from=navigator'
driver.get(url)
page_source = driver.page_source
二、翻译
"""
翻译系统设计
1. 用户输入
2. 内容翻译
3. 翻译结果输出
"""
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
def trans(word):
options = Options()
options.add_argument("--headless")
driver = WebDriver(executable_path='chromedriver.exe', options=options)
driver.get("http://fanyi.youdao.com/")
input_element = driver.find_element_by_id("inputOriginal")
input_element.send_keys(word)
sleep(3)
result_container = driver.find_element_by_id("transTarget")
result = result_container.find_element_by_xpath("./p/span")
print(result.text)
if __name__ == '__main__':
trans("开心")
import requests
def trans_to_en(word):
url = "http://app.ifanyi.com.cn/translate?callback=fanyiCallback"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"host": "app.ifanyi.com.cn",
"origin": "http://www.ifanyi.com.cn",
"referer": "http://www.ifanyi.com.cn/",
}
data = {
"from": "en",
"to": "zh",
"formart": "json",
"type": "baidu",
"content": word,
}
response = requests.post(url, data=data, headers=headers)
result_json = response.json()
return result_json.get("trans_result")[0].get("dst")
if __name__ == '__main__':
print("欢迎来到我的翻译机")
while True:
word = input("请输入你要翻译的内容:")
result = trans_to_en(word)
print(result)
三、聊天机器人
import requests
def talk_with_robot(msg):
url = "http://openapi.tuling123.com/openapi/api/v2"
json_data = {
"perception": {
"inputText": {
"text": msg
},
},
"userInfo": {
"apiKey": "46bac4e4f1dc4469a24777e888e1f69b",
"userId": "110"
}
}
response = requests.post(url, json=json_data)
return response.json().get("results")[0].get("values").get("text")
if __name__ == '__main__':
print("欢迎来到小P机器人")
name = input("请给自己起个名字:")
print("您的名字已经设置好,欢迎您%s" % name)
while True:
msg = input("%s:" % name)
result = talk_with_robot(msg)
print("小P:%s" % result)
四、登录
from selenium.webdriver.chrome.webdriver import WebDriver
url = "https://passport.xinpianchang.com/login?redirect_uri=https%3A%2F%2Fwww.xinpianchang.com%2F"
driver = WebDriver(executable_path="chromedriver.exe")
driver.get(url)
login_phone = driver.find_element_by_id("login_phone")
login_phone.send_keys("18511287795")
login_password = driver.find_element_by_id("login_password")
login_password.send_keys("Rock1204")
login_btn = driver.find_element_by_xpath("//button[contains(@class, 'ant-btn')]")
print(login_btn)
login_btn.submit()