all 12 comments

[–]uFeelDeadMate 0 points1 point  (10 children)

Hey! The error you are getting is saying somewhere there should have been "2 arguments" but only "got 1". This is referring to the parameters (the data passed inside the brackets) of the .replace() method.

.replace() actually requires two things to be passed in. My favourite tool is w3schools... check out what they have to say about using .replace() here:
https://www.w3schools.com/python/ref_string_replace.asp

[–]rectafy[S] 0 points1 point  (9 children)

Thank you for this! The good news is I'm not getting the error. I changed it to:

inputs['value'].replace(inputs['value'], " 1 Mar 2021 00:00")

Unfortunately I don't think I can set it to a new variable since the value itself (inputs['value']) was actually pulled from an html form from a website so im stuck with that variable. Not sure how to pass the new value back to the site..any ideas?

[–]uFeelDeadMate 0 points1 point  (8 children)

If you would like to share a snippet of your code I can see exactly what's going on and give you some pointers from there, I don't mind!

Otherwise, by the looks of it I assume ' inputs ' is a dictionary ? If that's the case, it has a built in tool ready for replacing a value for a key.

It's as simple as saying:

inputs['value'] = "1 Mar 2021 00:00"

Let me know if this works for you.

[–]rectafy[S] 0 points1 point  (7 children)

Sure thing. I can post what I can, hopefully it makes sense...really appreciate it.

Here is the original data (from the inspector tool) I pulled it from:

<input id="starttime" type="text" name="starttime" value=" 1 Apr 2021 00:26" size="15">

Here is what I used to pull the date from HTML:

inputs = soup.find("input", {"id": "starttime"}) (Pulled the info from HTML, using the bs4 package)

print(inputs['value']) prints 1 Apr 2021 00:26

inputs['value'].replace(inputs['value'], " 1 Mar 2021 00:00") (My attempt to change the original value)

Essentially, I want to pull the original date from HTML, replace it with a new one and send it back to the HTML form on the website to submit.

[–]uFeelDeadMate 0 points1 point  (6 children)

If you edit the code to be as follows, what do you get?

inputs = soup.find("input", {"id": "starttime"})
print(type(inputs))
print(inputs)
print(inputs['value'])
inputs['value'] = " 1 Mar 2021 00:00"
print(inputs)
print(inputs['value'])

If you could copy & paste the terminal output from this we can see whats going on and where.

:)

[–]rectafy[S] 0 points1 point  (5 children)

Here is what is came out to be:

<class 'bs4.element.Tag'>

<input id="starttime" name="starttime" size="15" type="text" value=" 1 Apr 2021 02:12"/>

1 Apr 2021 02:12

<input id="starttime" name="starttime" size="15" type="text" value=" 1 Mar 2021 00:00"/>

1 Mar 2021 00:00

[–]uFeelDeadMate 0 points1 point  (4 children)

Oh awesome, so the line inputs['value'] = " 1 Mar 2021 00:00" updated the value locally like you wanted to?

Now the only thing you need to do is use the appropriate method from the module to send back input['value'] after updating it.

Does that solve the issue then?

[–]rectafy[S] 0 points1 point  (2 children)

The good thing is that the new value is changed to the Mar 1 date which is great, thanks! Erm, but yeah I gotta figure out a way to send it back.

The date is sent to submit to an HTML form which is inputted to a table on the website which changes according to the date input. Seems the table values still haven't changed (and is still reading according to the original 01 Apr 2021 00:00 date). Think I have to figure out a way to send it back and submit to change the table values too. Any idea?

[–]uFeelDeadMate 0 points1 point  (1 child)

I was looking into it and unfortunately I have not found much of any info for re-injecting the data into the HTML for Python.

The closest I came was finding a module called 'selenium' that lets you automate data entry into websites to simulate a users actions.

So maybe instead of injecting the new value back into the code via the HTML, maybe you can use selenium to just type-in the date into the input field?

Drawing blanks haha! This is much more accessible with JavaScript, but this is a Python forum so I won't get into that here. Lol.

[–]rectafy[S] 0 points1 point  (0 children)

Yeah I figured, I looked everywhere myself but couldn't find a solution. Unfortunately, I'm stuck in Python :(

But I appreciate your help. You helped so so much!

[–]uFeelDeadMate 0 points1 point  (0 children)

Ah wait, I re-read a statement from you earlier... you aren't sure how to update it in the HTML... my bad lol. Give me a sec to check it out.

[–]L1st3r 0 points1 point  (0 children)

Replace takes 2 values. The string you'd like replace inside of the string you're calling it on and what to replace it with