Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxml.etree.ParserError: Document is empty - how to handle this issue? #191

Open
natesanshreyas opened this issue Feb 13, 2025 · 0 comments

Comments

@natesanshreyas
Copy link

This is the script I'm using:

import myfitnesspal
import browser_cookie3
import csv
import pandas as pd
import os.path
from datetime import date,timedelta, datetime
import os.path

def mfp_to_csv():
client = myfitnesspal.Client(browser_cookie3.chrome())
response = client.session.get(client._get_url_for_measurements())
print(response.text) # See what the raw response looks like

end_date = date(date.today().year, 2, 13)
end_date2 = date.today()
start_date = date(date.today().year, 1, 23)
delta = timedelta(days=1)
mfp_date = []
mfp_calories = []
mfp_fats = []
mfp_carbs = []
mfp_protein = []
mfp_weight = []

current_date = start_date

while current_date <= end_date: #in the timespan of ten days ago until now
    mfp_time = client.get_date(current_date) #assigns variable for the command that gets all user macros for the specified timespan
    mfp_time_str = mfp_time.date.strftime("%m-%d-%Y")
    mfp_measurement = client.get_measurements(measurement= "Weight", lower_bound= current_date, upper_bound = current_date) #assigns variable for the command that gets all user weights for the specified timespan
    if current_date in mfp_measurement: #if the date is in the dict for weight
        mfp_weight.append(mfp_measurement.get(current_date,0)) #the 
    else:
        mfp_weight.append("NaN")
        
    mfp_date.append(mfp_time_str)
    mfp_calories.append(mfp_time.totals.get('calories',0))
    mfp_fats.append(mfp_time.totals.get('fat',0))   
    mfp_carbs.append(mfp_time.totals.get('carbohydrates',0))
    mfp_protein.append(mfp_time.totals.get('protein',0))

    current_date += delta

data = {
    "Date": mfp_date,
    "Weight": mfp_weight,
    "Fats": mfp_fats,
    "Carbs": mfp_carbs,
    "Protein": mfp_protein,
    "Calories": mfp_calories,
    
}

with open('mfp_csv.csv', 'w+', newline = '') as f:
    writer = csv.writer(f)
    reader = csv.reader(f)
    headers = ["Date", 'Calories', 'Carbohydrates', 'Fat', 'Protein']
    df = pd.DataFrame(data)
    df.to_csv("mfp_csv.csv", index = False)

mfp_to_csv()

This is the error I'm getting:

Traceback (most recent call last):
File "/Users/shreyas.natesan/Documents/mfp.py", line 122, in
mfp_to_csv()
File "/Users/shreyas.natesan/Documents/mfp.py", line 36, in mfp_to_csv
mfp_measurement = client.get_measurements(measurement= "Weight", lower_bound= current_date, upper_bound = current_date) #assigns variable for the command that gets all user weights for the specified timespan
File "/Users/shreyas.natesan/Library/Python/3.9/lib/python/site-packages/myfitnesspal/client.py", line 612, in get_measurements
document = self._get_document_for_url(self._get_url_for_measurements())
File "/Users/shreyas.natesan/Library/Python/3.9/lib/python/site-packages/myfitnesspal/client.py", line 288, in _get_document_for_url
return lxml.html.document_fromstring(content)
File "/Users/shreyas.natesan/Library/Python/3.9/lib/python/site-packages/lxml/html/init.py", line 761, in document_fromstring
raise etree.ParserError(
lxml.etree.ParserError: Document is empty

Has anyone run into this before? Any thoughts on how to get around this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant