import requests
response = requests.get('https://www.baidu.com')
code = response.status_code
print(code)
if code == 200:
print('请求成功,后续获取返回的数据')
else:
print('请求失败')
response.encoding = 'utf-8'
data = response.text
print(data)
import requests
def get_json_data():
"""
获取json数据
:return:
"""
response = requests.get('http://api.tianapi.com/txapi/ncovabroad/index?key=c9d408fefd8ed4081a9079d0d6165d43')
if response.status_code == 200:
data = response.json()
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:
with open('img/0.jpg', 'wb') as f:
f.write(response.content)
else:
print('请求失败')
if __name__ == '__main__':
get_bytes_data()
import requests
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'
arguments = {'key': 'c9d408fefd8ed4081a9079d0d6165d43'}
response = requests.get(url, params=arguments)
print(response.headers)
print(response.json())
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)
else:
print('请求失败')
print(response)
def jie_pai():
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×tamp=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
b = webdriver.Chrome()
url = 'https://www.51job.com/'
b.get(url)
content = b.page_source
print(content)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
b = webdriver.Chrome()
url = 'https://www.51job.com/'
b.get(url)
"""
浏览器对象.find_element_by_id(id值)
浏览器对象.find_element_by_css_selector(css选择器) - 获取选择器选中的一个标签(第一个),返回值是标签对象
浏览器对象.find_elements_by_css_selector(css选择器) - 获取选择器选中的所有标签,返回值是列表,列表中元素是标签
"""
input = b.find_element_by_css_selector('#kwdselectid')
print(input)
input.send_keys('数据分析')
button = b.find_element_by_css_selector('body > div.content > div > div.fltr.radius_5 > div > button')
button.click()
print(b.page_source)