Python网络爬虫--selenium

一、打开网页获取页面源码

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):
    # 浏览器驱动的配置信息  Chrome浏览器
    options = Options()
    # 向options中添加参数
    options.add_argument("--headless")

    # 构建WebDriver的时候 指定自己的配置信息
    driver = WebDriver(executable_path='chromedriver.exe', options=options)
    driver.get("http://fanyi.youdao.com/")
    # 根据id 查找元素
    input_element = driver.find_element_by_id("inputOriginal")
    # 向允许输入的元素中传入数据
    input_element.send_keys(word)

    sleep(3)
    # 根据id 查找元素
    result_container = driver.find_element_by_id("transTarget")
    # 根据xpath 进行元素内部查询
    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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值