[Python] Using beautiful soup (or maybe some other library) to scrape data from an .aspx...

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I was wondering if someone could help me out with a web scraping problem.. I am new to both python and web scraping..

    I am trying to use the below python program to scrape data from the following webpage,

    "https://www.cocorahs.org/ViewData/ListDailyPrecipReports.aspx"

    but I keep getting a Runtime error "table#ucReportList_ReportGrid not found" on the raise RuntimeError line (3rd last line).

    Now, this program works perfectly fine for a webpage without checkboxes.. However, the webpage I am trying to scrape the data from has some checkboxes and I think that's exactly the part I'm messing up with..

    I did a view page source and looked up the html tag names but I feel I am missing something very conceptual.. I am wondering what it might be !!

    The two lines after the FL-BV-163 line are the ones I am worried about..

    Below is the code. What changes should I make/add to it ? Should I be using some more libraries ? Thank You !!

    import requests
    from bs4 import BeautifulSoup
    from requests_html import HTMLSession

    import pandas as pd
    from io import StringIO

    from datetime import datetime

    session = requests.Session()

    response = session.get('https://www.cocorahs.org/ViewData/ListDailyPrecipReports.aspx')

    soup = BeautifulSoup(response.content, "html.parser")
    view_state = soup.find("input", {"name": "__VIEWSTATE", "value": True})["value"]
    view_state_generator = soup.find("input", {"name": "__VIEWSTATEGENERATOR", "value": True})["value"]
    event_validation = soup.find("input", {"name": "__EVENTVALIDATION", "value": True})["value"]

    response = session.post('https://www.cocorahs.org/ViewData/ListDailyPrecipReports.aspx', data={
    "__EVENTTARGET": "",
    "__EVENTARGUMENT": "",
    "__LASTFOCUS": "",
    "VAM_Group": "",
    "__VIEWSTATE": view_state,
    "VAM_JSE": "1",
    "__VIEWSTATEGENERATOR": view_state_generator,
    "__EVENTVALIDATION": event_validation,
    "obsSwitcher:ddlObsUnits": "usunits",
    "frmPrecipReportSearch:ucStationTextFieldsFilter:tbTextFieldValue": "FL-BV-163",
    "frmPrecipReportSearch:ucStationTextFieldsFilter:cblTextFieldsToSearch:0": "checked",
    "frmPrecipReportSearch:ucStationTextFieldsFilter:cblTextFieldsToSearch:1": "",
    "frmPrecipReportSearch:ucStateCountyFilter:ddlCountry": "allcountries",
    "frmPrecipReportSearch:ucDateRangeFilter:dcStartDate:di": "6/13/2025",
    "frmPrecipReportSearch:ucDateRangeFilter:dcStartDate:hfDate": "2025-06-13",
    "frmPrecipReportSearch:ucDateRangeFilter:dcEndDate:di": "6/16/2025",
    "frmPrecipReportSearch:ucDateRangeFilter:dcEndDate:hfDate": "2025-06-16",
    "frmPrecipReportSearch:ddlPrecipField": "GaugeCatch",
    "frmPrecipReportSearch:ucPrecipValueFilter:ddlOperator": "LessEqual",
    "frmPrecipReportSearch:ucPrecipValueFilter:tbPrecipValue:tbPrecip": "0.15",
    "frmPrecipReportSearch:btnSearch": "Search",
    })

    table = BeautifulSoup(response.content, "html.parser").find("table", id="ucReportList_ReportGrid")

    if table is None:
    raise RuntimeError("table#ucReportList_ReportGrid not found")

    df = pd.read_html(StringIO(str(table)))[0]

    print(df)

    Continue reading...

Compartilhe esta Página