3u3an 2022-01-07 16:37 采纳率: 100%
浏览 72
已结题

python抓取405错误

刚学习了python,写了一个小脚本想抓取API接口的 json数据,并提取到处excel表。但是一直报405错误
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
from bs4 import BeautifulSoup
from urllib import request
import urllib.parse 
import json
import openpyxl

def get_data(search_url):
    data = {}
    
    data = urllib.parse.urlencode(data).encode('utf-8')
    # requests.add_header('Cookie',cookie)
    html = urllib.request.urlopen(search_url, data)
     # response = urlopen(requests)
    print(html)
    html = BeautifulSoup(response, 'html.parser', from_encoding='utf-8')
    # print(html)
    return html
 
def handle_data(html):
    # get all text from html
    rawdata = html.get_text()
    print(rawdata)
    json1 = json.loads(rawdata)
   
    json2 = json1['userDataTs']
    print(json2)
    json3 = json2['userData']
    print(json3)
    json4 = json3['8']
    print(json4)
    
    result = []
    for num in range(0,len(json2)):
        result_part=[]
        time= json2[num]['8']
        data=json4[num]['gSensors']

        result_part.append(8)
        result_part.append(gSensors)
    print(result)
    return result
 
 
#新建excel
def creatwb(wbname):  
    wb=openpyxl.Workbook()
    wb.save(filename=wbname)
    print ("新建Excel:"+wbname+"成功")
 
 
def savetoexcel(result, fields, wbname):
    creatwb(wbname)
    wb = openpyxl.load_workbook(filename=wbname)
 
    sheet1 = wb.active #  获取默认打开的(active)的WorkSheet
 
    field=1
    for field in range(1,len(fields)+1):   # 写入表头
        _=sheet1.cell(row=1,column=field,value=str(fields[field-1]))
 
    row1=1
    col1=0
    for row1 in range(2,len(result)+2):  # 写入数据
        for col1 in range(1,len(result[row1-2])+1):
            _=sheet1.cell(row=row1,column=col1,value=(result[row1-2][col1-1]))
 
    wb.save(filename=wbname)
    print("保存成功")
 
if __name__=='__main__':
    # curl查看
    # curl http://localhost:44444/beacons?macs=cc0003003551
 
    url = "http://127.0.0.1:44444/beacons?macs=cc0003003551"
    html = get_data(url)
    result = handle_data(html)
 
    fields = ['8','gSensors']
    wbname = './filename.xlsx'
    savetoexcel(result, fields, wbname)

输出错误结果

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\web.py", line 78, in
html = get_data(url)
File "C:\Users\Administrator\Desktop\web.py", line 15, in get_data
html = urllib.request.urlopen(search_url, data)
File "D:\Python\Python310\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "D:\Python\Python310\lib\urllib\request.py", line 525, in open
response = meth(req, response)
File "D:\Python\Python310\lib\urllib\request.py", line 634, in http_response
response = self.parent.error(
File "D:\Python\Python310\lib\urllib\request.py", line 563, in error
return self._call_chain(*args)
File "D:\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "D:\Python\Python310\lib\urllib\request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 405: Method Not Allowed

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2022-01-07 18:31
    关注

    建议使用requests,添加参数headers,cookies,params,这样试一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月20日
  • 已采纳回答 1月12日
  • 创建了问题 1月7日

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。