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

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 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题
  • ¥15 python,pyopenGL的OBV渲染无法设置透明
  • ¥20 matlab可以把多个二维图合成为三维瀑布图吗
  • ¥15 EEPROM,软件i2c
  • ¥500 求解读该段JS代码,需要知道是用干什么的
  • ¥20 qt4代码实现二进制文件读取显示,以及显示的内容进行搜索
  • ¥15 Labview获取LK-G3001数据
  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿