下载CSV链接Python请求模块

2024-10-03 17:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试单击此站点上的“下载csv”按钮”https://fplreview.com/team-planner/#forecast_table'使用python请求。我曾尝试使用Chrome开发者工具来获取POST请求,这让我进入了这个页面,但当我点击按钮时,我看不到网络流量中发生了什么。 我正在执行一个初始帖子,并使用BeautifulSoup保存html页面,看起来我在正确的页面上,但如何实际保存csv却让我不知所措。我已在下面附上我的当前代码。谢谢 `

import requests
import bs4

with requests.Session() as s:
    url = 'https://fplreview.com/team-planner/'

    headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Referer": "https://fplreview.com/team-planner/"
    }

    data = {
        'HiveMind':'Yes',
        'Weeks': 5,
        'TeamID': 123
    }
    
    resp = s.post(url, data=data, headers=headers)

    soup = bs4.BeautifulSoup(resp.content,'lxml')
    with open('soup.html','w',encoding="utf-8") as file:
        file.write(str(soup))

`


Tags: csvhttpsimportcomdatahtml页面requests