python3实现获取excel中数据与发送get/post请求

从excel中读数据

引入了xlrd包,读取excel中第一列内容,具体行数由代码中变量i和row控制

# 读取excel中第一列内容,保存为list
workbook = xlrd.open_workbook_xls('gggg.xls')
sheet = workbook.sheet_by_index(0)

i = 2  # 从第几行开始
row = 67  # 到第几行结束

i -= 1  # 为了与excel中的行数保持一致
idList = {}
while i < row:
    val = sheet.cell(i, 0)
    idList[i] = val.value
    i = i + 1
print(idList)

发送get请求

request包来发送get请求,参数为url和headers

headers = {
    'Authorization': "admin"
}
url1 = "http://localhost:8080/api/test/getInfo" + str(theId)
response = requests.get(url=url1, headers=headers)
print(response.text)

发送post请求

request包发送post请求,这里需要把从get请求获取的json数据新增一个键值对,因此用json包做一个转换,json.loads是把str转为dict类型,json.dumps是把dict转换为str类型。

headers2 = {
    'Content-Type': 'application/json',
    'Authorization': "admin"
}
text = response.text
tt = json.loads(text)  # 把str类型转为dict类型
data = tt['data']
data['projectId'] = 1  # json新添加一个键值对
ttt = json.dumps(data)  # 转为字符串类型
body = ttt.encode('utf8')  # 编码设置为utf8
url2 = "https://localhost:8080/api/test/postInfo"
response2 = requests.post(url=url2, data=body, headers=headers2)
print(response2.text)

整体代码

import json
import time
import xlrd

from pip._vendor import requests

# 读取excel中第一列内容,保存为list
workbook = xlrd.open_workbook_xls('gggg.xls')
sheet = workbook.sheet_by_index(0)

i = 2  # 从第几行开始
row = 67  # 到第几行结束

i -= 1  # 为了与excel中的行数保持一致
idList = {}
while i < row:
    val = sheet.cell(i, 0)
    idList[i] = val.value
    i = i + 1
print(idList)

# 遍历idList,发送get与post请求
headers = {
    'Authorization': "admin"
}
headers2 = {
    'Content-Type': 'application/json',
    'Authorization': "admin"
}
for index in idList:
    time.sleep(1)  # 休眠1秒
    theId = idList[index]
    url1 = "http://localhost:8080/api/test/getInfo" + str(theId)
    response = requests.get(url=url1, headers=headers)
    print(response.text)
    text = response.text
    tt = json.loads(text)  # 把str类型转为dict类型
    data = tt['data']
    data['projectId'] = 1  # json新添加一个键值对
    ttt = json.dumps(data)  # 转为字符串类型
    body = ttt.encode('utf8')  # 编码设置为utf8
    url2 = "https://localhost:8080/api/test/postInfo"
    response2 = requests.post(url=url2, data=body, headers=headers2)
    print(response2.text)
print("全部插入完成,嘿嘿嘿")

实现效果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Python 编写爬虫来获取企业数据。以下是一个简单的步骤: 1. 导入所需的库:常用的库包括 requests, BeautifulSoup 和 pandas。 2. 发送 HTTP 请求:使用 requests 库发送 GET 或 POST 请求获取企业数据所在的网页源代码。 3. 解析 HTML:使用 BeautifulSoup 库解析网页源代码,提取出感兴趣的数据。 4. 数据处理与存储:使用 pandas 库进行数据处理和清洗,并将数据保存到合适的格式(如 CSV 文件Excel 表格等)。 下面是一个简单的示例代码,用于从一个网页上提取企业名称和电话号码: ```python import requests from bs4 import BeautifulSoup import pandas as pd # 发送 HTTP 请求获取网页源代码 response = requests.get('http://example.com') html = response.text # 使用 BeautifulSoup 解析 HTML soup = BeautifulSoup(html, 'html.parser') # 提取企业名称和电话号码 company_name = soup.find('span', {'class': 'company-name'}).text phone_number = soup.find('span', {'class': 'phone-number'}).text # 创建 DataFrame 对象 data = {'Company Name': [company_name], 'Phone Number': [phone_number]} df = pd.DataFrame(data) # 将数据保存到 CSV 文件 df.to_csv('company_data.csv', index=False) ``` 请注意,具体的爬虫实现方法会因不同的网站结构而有所不同。在实际应用,你可能需要处理页面跳转、登录验证、反爬虫机制等问题。此外,务必遵守网站的使用规则,并尊重数据的版权和隐私。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值