Instaloader download_profiles with post_filter example? - instaloader

I'm trying to download a profile with posts after a certain date as a test. I have the simple code:
L = instaloader.Instaloader(save_metadata=False, download_comments=False)
L.download_profiles(Profiles, posts=True, fast_update=True, post_filter="date_utc >= datetime(2019, 1, 1)")
It works fine without the post_filter, but with the post_filter, I get:
<Post BOJaRXwhIix> skipped. Filter evaluation failed: 'str' object is not callable
<Post BOJZ6nqhpkX> skipped. Filter evaluation failed: 'str' object is not callable
<Post BHDC70ShoOs> skipped. Filter evaluation failed: 'str' object is not callable
I think I'm missing something obvious, but the documentation says that the post_filter string should be a Python-legal boolean statement, and I got this example directly from the command-line filter example in the docs. Advice?

L.download_profiles(Profiles, posts=True, fast_update=True, post_filter=lambda post: post.date_utc >= datetime(2019, 1, 1))

Related

Twitterscrapper error solution is throwing a different error

if i use twitterscraper-1.6.1
list_of_tweets = query_tweets('ukraine', limit=10, begindate=dt.date.today(), enddate=dt.date.today(), poolsize=20, lang='en')
i get AttributeError: 'NoneType' object has no attribute 'find_all'
if i use twitterscraper-0.2.7 as suggested at Import of twitterscraper returns 'NonType' error
i get unexpected keyword 'begindate'
I am trying to pull tweets using https://github.com/taspinar/twitterscraper and following along with the examples.

WebElement' object is not subscriptable [duplicate]

I try to press the Replay button at Spotify Web Player with Python, but get this error. How can I press buttons in a webplayer?
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
replay.click()
Error:
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
TypeError: 'WebElement' object is not subscriptable
This error message...
TypeError 'WebElement' object is not subscriptable
...implies that you have attached an index to a WebElement which is not supported.
Analysis
Only list elements can be indexed. However, in this line of code:
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""") would always return a single WebElement. Hence you can't access the element through any index, e.g. [0], [1], etc as an index can be associated only with a list.
Solution
There are two approaches to solve the issue.
In the first approach, you can remove the index, i.e. [0], and in that case replay will be assigned with the first matched element identified through locator strategy as follows:
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")
In the other approach, instead of using find_element_by_xpath() you can create a list using find_elements_by_xpath() and access the very first element from the List using the index [0] as follows:
replay = driver.find_elements_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
Reference
You can find a couple of relevant discussions in:
Exception has occurred: TypeError 'WebElement' object is not subscriptable
find_element_by_xpath
returns first found element (not array)
find_element_by_xpath(...).click()
or
find_elements_by_xpath(...)[0].click()
For everybody, who get this error: you might have confused driver.find_element() with driver.find_elements() and you are trying to get the singular item from WebElement object, not from a list Check it in your code
driver.find_elements() returns a list of appropriate WebElements
There is also another function - driver.find_element() and you can say that:
driver.find_element() = driver.find_elements()[0]
In this example Python is trying to get the first item from driver.find_element() object, which returns a WebElement object, not a list
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
As #KunduK commented remove the [0].
You are using absolute xPath this is not recommended.
Try using relative xPath...
If there are a few buttons and you need the first use the [0] in the xpath like so:
replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button[0]""")
replay.click()

I am getting this kind of error whenever i running this code

My Code:-
one_hot_encoder = OneHotEncoder(categorical_features=[0])
The Error shown:
TypeError: __init__() got an unexpected keyword argument 'categorical_features'
OneHotEncoder doesn't have a parameter name categorical_features.
After defining the one_hot_encoder=OneHotEncoder() object, use the fit_transform() method to encode the categorical data.
Visit this for more..

how to access the individual elements of a python <class 'requests.models.Response'> class

My code accesses a light sensor via a python request:
address = 'https://api.particle.io/v1/devices/my_device_id/analogvalue'
headers = {'Authorization':'Bearer {0}'.format(access_token)}
vals = requests.get(address, headers=headers)
The code returns the following values:
{"cmd":"VarReturn","name":"analogvalue","result":171,"coreInfo":{"last_app":"","last_heard":"2019-06-13T21:55:57.387Z","connected":true,"last_handshake_at":"2019-06-13T20:51:02.691Z","deviceID":"my_device_id","product_id":6}}
Python tells me that this is a 'requests.models.Response' class and not a dictionary like I thought.
When I try to access the 'result' value, I get error messages. Here are the various ways I have tried along with their error messages.
print(vals[2])
TypeError: 'Response' object does not support indexing
print(vals['result'])
TypeError: 'Response' object is not subscriptable
print(vals[2].json())
TypeError: 'Response' object does not support indexing
print(vals['result'].json())
TypeError: 'Response' object is not subscriptable
I got the last two approaches (.json) from a answer here on stack overflow.
Can anyone tell me how to access this result value or am I going to be forced to use regular expression?
EDIT: With help from Sebastien D I added the following and was able to get the result I was looking for.
import json
new_vals = json.loads(vals.content)
print(new_vals['result'])
Just do :
import json
### your code ###
json.loads(vals.content)

Getting "AttributeError: 'NoneType' object has no attribute 'loc'"

I am calculating the sum of columns using loc, but I am getting the following error:
AttributeError: 'NoneType' object has no attribute 'loc'
Pasted below is my code:
top_C2_index = sum(sub_sector_df1.loc['C2']['Contribution%'].cumsum(axis=0) < 80)+1
Any help would be greatly appreciated!
Your sub_sector_df1 hasn't been instantiated with a value, i.e. its value is of type None by default then. Can you paste how you're instantiating sub_sector_df1?
Note: I don't have enough privilege to comment..
Hopefully that makes sense!

Resources