requests 可以玩接口自动化测试,爬虫也是可以滴

import requests
#1.带参的get请求:
url ='URL_你的'
requests.get(url,params={"key":"value"})
#带参的post请求(表单提交):
requests.post(url,data={"key1":"value1","key2":"value2"})
#josn参数提交:
requests.post(url,json={"key1":"value1","key2":"value2"})
response = requests.get(url)
print(response.headers) # 头head
print(response.text)  #body,直接从网络上面抓取的数据,经过编码打印出来
print(response.content)  #直接从网络上面抓取的数据,没有经过任何解码
print(response.json()) #json 直接从网络上面抓取的数据,转换成字典模式展示
print(response.json()['key'])# 查字典的方式展示一个value
print(response.status_code) #请求状态码
print(response.reason) #状态码的含义
print(response.elapsed) #请求响应时间
print(response.request)  #查看api请求信息是什么请求方式
print(response.encoding)  #查看内容编码
print(response.raw.read(100000))  #查看前10字节的内容

记录额外小知识1,大神绕道:

from selenium import webdriver
import time
import urllib.request
driver = webdriver.Chrome()
driver.get('http://www.l99.com/EditText_view.action?textId=9458460')
time.sleep(2)
for i in range(1,16):
    yuansu = '//*[@id="db_postion_49735813"]/div/div[1]/div/p[' + str(i) + ']/span/img'
    print(yuansu)
    yuansu = driver.find_element_by_xpath(yuansu)
    attribute=yuansu.get_attribute("src")#取元素的元素值,这里取的就是图片的打开地址
    print(attribute)
    path_my='C:\\Users\\Administrator\\Desktop\\qq\\'+str(i)+'.jpg'
    urllib.request.urlretrieve(attribute,path_my)
driver.close()

记录额外小知识2,大神绕道:

import urllib.request
# 网络上图片的地址
img_src = 'http://s10.sinaimg.cn/mw690/002FPe2pgy6ZLAOby0p29&690'
path_my='C:\\Users\\Administrator\\Desktop\\qq\\qqq.jpg'
urllib.request.urlretrieve(img_src,path_my)

猜你喜欢

转载自www.cnblogs.com/Mr-Simple001/p/10540667.html