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

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条)

相关推荐 更多相似问题