【问题标题】:Href attribute not showing for <a> tag when using requests.post使用 requests.post 时未显示 <a> 标签的 Href 属性
【发布时间】:2016-09-19 06:22:12
【问题描述】:

当我提交数据时,我正在尝试下载出现在此page 上的 .csv 文件

数据:安全性价格交易量和可交付头寸数据
符号 : 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"])

现在在检查元素时我看到了这个

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

【问题讨论】:

    标签: python html beautifulsoup python-requests


    【解决方案1】:

    要获得链接,您必须单击按钮,因此您可以使用 selenium 或类似的东西,但很容易自己解析数据,因为您从发布请求中得到的只是数据:

    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)
    

    data.csv 的 sn-p:

    Symbol,Series,Date,Prev Close,Open Price,High Price,Low Price,Last Price,Close Price,VWAP,Total Traded Quantity,Turnover in Lacs,No. of Trades,DeliverableQty,% Dly Qt toTraded Qty
    3IINFOTECH,EQ,23-May-2014,9.90,10.25,10.70,9.70,10.10,10.10,10.23,"84,99,408",869.20,"16,539","40,35,648",47.48
    3IINFOTECH,EQ,26-May-2014,10.10,10.40,10.60,9.10,9.30,9.20,9.97,"59,15,990",589.88,"9,894","27,10,021",45.81
    3IINFOTECH,EQ,27-May-2014,9.20,9.20,9.30,8.30,8.60,8.55,8.53,"34,95,072",298.18,"3,600","14,71,141",42.09
    3IINFOTECH,EQ,28-May-2014,8.55,8.60,9.40,8.45,9.30,9.15,9.07,"36,09,261",327.27,"3,955","13,92,733",38.59
    3IINFOTECH,EQ,29-May-2014,9.15,9.25,9.50,8.80,9.40,9.35,9.28,"30,13,036",279.69,"3,090","15,20,654",50.47
    3IINFOTECH,EQ,30-May-2014,9.35,9.35,9.55,8.90,9.00,9.00,9.13,"13,97,140",127.53,"1,992","7,43,964",53.25
    

    【讨论】:

    • 表中的数据实际上是不完整的。原来整个数据是在一个隐藏的 div 中发送的,我可以从中解析和提取。不过感谢您的回答。
    • @AbhyudayaSrinet,你不是在说div id="csvContentDiv"
    • 是的,但这会出现在对同一链接的获取请求中,例如此处https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol=RELIANCE&amp;segmentLink=3&amp;symbolCount=2&amp;series=ALL&amp;dateRange=24month&amp;fromDate=&amp;toDate=&amp;dataType=PRICEVOLUMEDELIVERABLE。很抱歉造成混乱
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    相关资源
    最近更新 更多