pandas 如何将URL响应转换为 Dataframe

lhcgjxsq  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(145)

我一直在处理一个需求,我需要从一个网站下载文件,该网站以csv格式输出数据,然后将其写入SQL表。我使用以下逻辑从网站下载数据。

import urllib
from bs4 import BeautifulSoup

url = "https://abcdef.oz/login"
response = opener.open(url)
if response.status == 200:
    #webUrl = urllib.request.urlopen(url)
    #print("Result code: " + str(webUrl.getcode()))
    data = response.read().decode("utf-8")
    soup = BeautifulSoup(data)

    #Got a token
    token = soup.find_all("meta", {"name":"token"}, limit=1)[0]['content']
    print("token",token)
    sdata = {"email": "abc@def.com", "password": "password"}

    data = urllib.parse.urlencode(sdata).encode()

    print("data",data)
    url = "https://abcdef.oz.co/reports"

    response = opener.open(url)
    r = response.read().decode('utf-8')

    print(r)
 else:
    print("No Response")

现在如何将响应转换为一种格式,以便跳过头并将数据写入SQL表?
响应的输出如下所示

"Col1","Col2","Col3"\n"abc","def","efg"\n"hij","klm","mno"

先谢了

mklgxw1f

mklgxw1f1#

这并不令人兴奋,但你试过:

df = pandas.read_html(url)

相关问题