我提交数据时,Href属性未显示<a> tag when using requests.post

时间:2016-05-22 10:01:26

标签: python html beautifulsoup python-requests

I am trying to download the .csv file that appears on this page

数据:安全价格和&amp;可交割头寸数据
符号:3INFOTECH
选择系列:全部
期限:24个月

我的代码是

symbol = "3IINFOTECH"
url = "https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp"
data = {

    "dataType":"priceVolumeDeliverable",
    "symbol":symbol,
    "segmentLink":"3",
    "symbolCount":"2",
    "series":"ALL",
    "rdPeriod":"groupPeriod",
    "dateRange":"24month"
}

headers = {
   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
}
print("fetching for " + symbol)
session = requests.session()
response = requests.post(url, data, headers = headers)
html_content = response.text

soup = BeautifulSoup(html_content, "html.parser")

download_link = soup.findAll("span", attrs = {"class":"download-data-link"})[0]
print(download_link.a["href"])

现在检查元素我看到了这个 enter image description here

如何下​​载csv文件?我的代码中的post请求没有显示href属性。

enter image description here

1 个答案:

答案 0 :(得分:1)

要获取链接,您必须单击按钮,这样您就可以使用selenium或类似的东西,但是很容易自己解析数据,因为您从post请求中获得的所有内容都是数据:

@media screen and (max-width: 416px) {
   .container {
        flex-wrap: wrap;
    }
   .img-c {
        width: 50%;
        border: 2px solid white;
        box-sizing: border-box;
    }
}

一段data.csv:

symbol = "3IINFOTECH"
url = "https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp"
data = {

    "dataType": "priceVolumeDeliverable",
    "symbol": symbol,
    "segmentLink": "3",
    "symbolCount": "2",
    "series": "ALL",
    "rdPeriod": "groupPeriod",
    "dateRange": "24month"
}

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
}
print("fetching for " + symbol)
import csv
response = requests.post(url, data, headers=headers)
html_content = response.text
soup = BeautifulSoup(html_content, "html.parser")
cols = [th.text for th in soup.select("th")]

rows = ([td.text for td in row.select("td")] for row in soup.select("tr  + tr"))

with open("data.csv", "w") as f:
    wr = csv.writer(f)
    wr.writerow(cols)
    wr.writerows(rows)