python第二十二天

import requests

# requests: 是python用来发送http请求的一个第三方库

# 1.发送请求获取响应
# requests.get(url)  -  发送get请求
# requests.post(url)  -  发送post请求
response = requests.get('https://www.baidu.com')

# 2.获取响应内容
# 1)状态码
code = response.status_code
print(code)
if code == 200:
    print('请求成功,后续获取返回的数据')
else:
    print('请求失败')

# 2)修改返回数据的编码方式(一般针对文本数据乱码的时候)
# 设置的值是网页源代码中charset的值
response.encoding = 'utf-8'

# 3)获取响应体(服务器返回的数据)
# a.获取文本数据(返回数据是网页源代码)
data = response.text
print(data)

# b.获取json数据(返回的数据是json格式的数据)
# response.json()

# c.获取二进制数据(下载图片)
# response.content
import requests


def get_json_data():
    """
    获取json数据
    :return:
    """
    # 1)发送请求
    response = requests.get('http://api.tianapi.com/txapi/ncovabroad/index?key=c9d408fefd8ed4081a9079d0d6165d43')

    # 2)获取数据
    if response.status_code == 200:
        data = response.json()
        # print(data)
        for x in data['newslist']:
            print(f'{x["provinceName"]}:{x["confirmedCount"]}')
    else:
        print('请求失败')


def get_bytes_data():
    """
    下载图片
    :return:
    """
    # 发送请求
    response = requests.get('https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2866940675,2065857896&fm=26&gp=0.jpg')
    # 获取二进制数据
    if response.status_code == 200:
        # print(response.content)
        with open('img/0.jpg', 'wb') as f:
            f.write(response.content)
    else:
        print('请求失败')


if __name__ == '__main__':
    get_bytes_data()
import requests


# =============1.参数=============
def argument():
    """
    参数:发送请求的时候客户端发送给服务器的数据就是参数
    传参的方式:
        1.拼接url(GET) - 将参数以 '参数名=值'的形式 通过?拼接到url的后面,多个参数用&连接
        例如:http://api.tianapi.com/txapi/ncovabroad/index?key=c9d408fefd8ed4081a9079d0d6165d43
        2.给参数params赋值(GET/POST) - 将所有的参数以键值对的形式创建一个字典,然后将字典赋给params
    """
    # 方法一:
    # url = 'http://api.tianapi.com/txapi/ncovabroad/index?key=c9d408fefd8ed4081a9079d0d6165d43'
    # response = requests.get(url)

    # 方法二:
    url = 'http://api.tianapi.com/txapi/ncovabroad/index'
    arguments = {'key': 'c9d408fefd8ed4081a9079d0d6165d43'}
    response = requests.get(url, params=arguments)

    # 获取响应头
    print(response.headers)

    print(response.json())


# ======================2.请求头====================
# requests.get/post(url/请求地址, params/参数,...,headers/请求头, proxy/代理)
def request_header():
    url = 'https://movie.douban.com/top250'
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
    }
    response = requests.get(url, headers=header)
    if response.status_code == 200:
        print(response.text)
    else:
        print('请求失败')
        print(response)


def zhihu():
    url = 'https://www.zhihu.com/hot'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
        'Cookie': '_zap=7cb5f066-c7a7-4caa-81ae-546d1b04f582; _xsrf=2cebe102-78c9-4db0-a1ab-926ca40c56dd; d_c0="AACfjl6ZGxKPTqfKLslQo1EHf9SI1JL4zM0=|1603869789"; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1602404920,1602420847,1603869790; capsion_ticket="2|1:0|10:1603869869|14:capsion_ticket|44:N2M0Zjk4MTI1YTc0NGQ5OTlkODhhZjJlZGZhNTVjOWY=|81b7f0161e73737706fe859b6836094d7dc791ba8aa8f175da6589c4dc5a7944"; z_c0="2|1:0|10:1603869900|4:z_c0|92:Mi4xaW5CWUdRQUFBQUFBQUotT1hwa2JFaWNBQUFDRUFsVk56S25BWHdEb2VFc05XWHFud1doZk85dFJaRXJKZ0MwWG5n|52ca47100a6fc53e99bafe19ef31c36a352d3ad8cad6814027477fcb13b3b3aa"; tshl=; SESSIONID=PZyCrfwkuPdH8bsX9hEozOIhDy7o4tnq7fAUxzYpX1M; JOID=UlwdCkM0jSwuJCDQVD0hfVletQxNYNtUbF5Cqz5J2URnHUqnAOactXgoKN1U62oXmzPUJyg1FOkGb6RbtlEuK6g=; osd=VlAQAk8wgSEmKCTcWTUteVVTvQBJbNZcYFpOpjZF3UhqFUajDOuUuXwkJdVY72Yakz_QKyU9GO0KYqxXsl0jI6Q=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1603870185; KLBRSID=37f2e85292ebb2c2ef70f1d8e39c2b34|1603870186|1603869788; tst=r'
    }
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        print(response.text)
        # with open('img/test.html', 'w', encoding='utf-8') as f:
        #     f.write(response.text)
    else:
        print('请求失败')
        print(response)


def jie_pai():
    # url = 'https://www.toutiao.com/search/?keyword=街拍'
    url = 'https://www.toutiao.com/api/search/content/?aid=24&app_name=web_search&offset=0&format=json&keyword=%E8%A1%97%E6%8B%8D&autoload=true&count=20&en_qc=1&cur_tab=1&from=search_tab&pd=synthesis&timestamp=1603871269830&_signature=_02B4Z6wo00f01oyQSKQAAIBCT9oGgCv25gaMlUwAAPzMMTK11zUZB3GB5sFAeqgHdbALqqiPRf1INhtfqzwz6ZBiYaBunTq4kERQGky1mKNTE0xiiaEvA7mYPeMHYdWd7Z6131O-lQfrpbgT22'
    response = requests.get(url)
    if response.status_code == 200:
        print(response.text)
    else:
        print('请求失败', response)


def bei_ke():
    url = 'https://cd.fang.ke.com/loupan'
    response = requests.get(url)
    print(response.text)


if __name__ == '__main__':
    bei_ke()
from selenium import webdriver

# 1.创建浏览器对象
# Chrome()/Firefox()/Safari()
b = webdriver.Chrome()

# 2.在浏览器中加载页面
url = 'https://www.51job.com/'
# url = 'https://movie.douban.com/top250'
b.get(url)

# 3.获取网页源代码
content = b.page_source
print(content)

# 4.关闭浏览器
# b.close()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

b = webdriver.Chrome()
url = 'https://www.51job.com/'
b.get(url)

# 1.获取已经加载的页面中的标签
"""
浏览器对象.find_element_by_id(id值)
浏览器对象.find_element_by_css_selector(css选择器)  -  获取选择器选中的一个标签(第一个),返回值是标签对象
浏览器对象.find_elements_by_css_selector(css选择器)  - 获取选择器选中的所有标签,返回值是列表,列表中元素是标签
"""
# input = b.find_element_by_id('kwdselectid')
input = b.find_element_by_css_selector('#kwdselectid')
print(input)

# 2.操作标签
# 1) 输入框中输入内容
input.send_keys('数据分析')
# input.send_keys(Keys.ENTER)

# 2) 点击按钮
button = b.find_element_by_css_selector('body > div.content > div > div.fltr.radius_5 > div > button')
button.click()

print(b.page_source)


# b.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值