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

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日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘