# Using Python script to extract data from HubSpot

**URL:** <https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [December 1, 2022, 1:53am UTC](https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323 "2022-12-01T01:53:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnWongCK](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JohnWongCK](https://community.hubspot.com/u/JohnWongCK)\
**Post date:** [December 1, 2022, 1:53am UTC](https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323/1 "2022-12-01T01:53:15Z")

</div>

Hi,

I have written a Python script to extract data from HubSpot using ACCESS TOKEN.

I **encountered** problem on extracting data based on last modified date as shown below.

```lia-code-sample
import requests
from datetime import datetime

dt_str = datetime(2022,10,1,0,0,0).strftime('%Y-%m-%d')

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type' : 'application/json',
 'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'lastmodifieddate',
 'operator' : 'GTE',
 'value' : dt_str}]}]}

response = requests.request('POST', url, headers=headers, json=data)

print(response)

```

The problem is

`<Response [400]>`

However, I **do not encounter** problem of extracting data based on last name and first name as shown below.

```lia-code-sample
import requests

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type' : 'application/json',
 'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'lastname',
 'operator' : 'EQ',
 'value' : 'Wong'}]}],
 'sorts' : [{'propertyName' : 'lastmodifieddate',
 'direction' : 'DESCENDING'}]}

response = requests.request('POST', url, headers=headers, json=data)

for i in range(10):
 print(response.json()['results'][i])
 print()

```

```lia-code-sample
import requests

url = 'https://api.hubapi.com/crm/v3/objects/contacts/search'

headers = {'content-type' : 'application/json',
 'authorization' : 'Bearer %s' % access_token}

data = {'filterGroups' : [{'filters' : [{'propertyName' : 'firstname',
 'operator' : 'EQ',
 'value' : 'John'}]}]}

response = requests.request('POST', url, headers=headers, json=data)

for i in range(5):
 print(response.json()['results'][i])
 print()

```

Hope to get advice on how I can proceed.

Thank you very much for any help rendered.

John

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [December 1, 2022, 1:48pm UTC](https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323/2 "2022-12-01T13:48:05Z")

</div>

Hey @JohnWongCK,

thank you for reaching out to the community. I haven’t tested my solution due to time constrains, but I’ve done similar things in the past.

The problem I suspect here is the datetime variable should be a UNIX timestamp in milliseconds, formatted as a string. So, the result would be, for example:

`time = "1669902197000"`

To use the datetime module, you could do something like this for the current time:

` currentTime = str(int(datetime.now().timestamp() * 1000))`

In your case, it should look some like this:

`time = str(int(datetime(2022,10,1,0,0,0).timestamp() * 1000))`

But again, I haven’t tested this yet with your object.

Please let me if this works for you.

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I’d really appreciate it.

Cheers,

Chriso

---

<div class="post-metadata">

**Author:** ![JohnWongCK](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JohnWongCK](https://community.hubspot.com/u/JohnWongCK)\
**Post date:** [December 5, 2022, 12:18am UTC](https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323/3 "2022-12-05T00:18:11Z")

</div>

Hi @ChrisoKlepke,

So sorry for my late reply as I was out of office last Friday.

Yes. I have tested your solution. It worked!

Thank you very much for your great advice.

I will also use your suggested code in my code.

Many thanks once again and have a good day!

John

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [December 5, 2022, 9:18am UTC](https://community.hubspot.com/t/using-python-script-to-extract-data-from-hubspot/70323/4 "2022-12-05T09:18:48Z")

</div>

Awesome, @JohnWongCK !

Thank you for letting me know if all worked out! Cheers, mate 🎉
