Web scripting invalid syntax in URL - python

I am a beginner of web scripting.
I was following a tutorial on Edureka: A Beginner’s Guide to learn web scraping with python!.
There is a syntax error shown inside the URL of my script:
driver.get('<a href='https://www.jbhifi.com.au/products/lenovo-ideapad-slim-5i-15-6-full-hd-laptop-512gb-intel-i5'>https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops?page=4')
The invalid syntax seems under com, which is very confusing to me.
I have no idea how to solve it.

The syntax error is because you are enclosing your string with single quotes, and you also have single quotes inside the string. So Python thinks that everything after '<a href=' is not a string, but it can't interpret that other stuff as Python code, so Python gives up and raises an error.
Normally you would deal with this by enclosing the string with double quotes, or by escaping the single quotes. However, with driver.get, you don't use the <a href="..."> part; you just give it the URL. So you can do this:
driver.get('https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops?page=4')

You cannot use same type of quotes inside and out within the same string. You can either use single and double quotes together or you can escape it. Modify your script as follows:
driver.get('<a href="https://www.jbhifi.com.au/products/lenovo-ideapad-slim-5i-15-6-full-hd-laptop-512gb-intel-i5">)

Related

Jupyter Notebook Code Block Not Working With Quotes and Special Characters

I am trying to wrap the following code with the markdown code block backticks:
users_pipeline = ["$match":{"created.user":{"$exists":True}}},
{"$group":{"_id":"$created.user",
"count":{"$sum":1}}},
{"$sort":{"count":-1}},
{"$limit":10}]
results = [doc for doc in db.Houston.aggregate(users_pipeline)]
But I think the quotes and special characters (like dollar sign and quotes) are getting in the way. I'm not sure how to work around this. I've tried using escape characters but it doesn't seem to improve
This is what it looks like when I run the cell:
I think you are missing an opening brace, (note the [{" before $match)
users_pipeline = [{"$match":{"created.user":{"$exists":True}}},
{"$group":{"_id":"$created.user",
"count":{"$sum":1}}},
{"$sort":{"count":-1}},
{"$limit":10}]
results = [doc for doc in db.Houston.aggregate(users_pipeline)]
would make much more sense?

python lxml xpath cannot parse apostrophe quote

I am trying to read a parameter from an XML file with folowing path:
parameter path in xml = "node1/node2/param's name"
parser = etree.parse(xml_file_path, etree.XMLParser(encoding='utf-8', recover=True, huge_tree=True))
parser.xpath("./SPLIT/NODE[contains(text(), 'node1')]/SPLIT/NODE[contains(text(), 'node2')]/SPLIT/NODE[contains(text(), 'param's name')]")
Due to a single quote in the node name, I am getting this eeror - {XPathEvalError}Invalid Expression
The same code works fine if there is no single quote.
I tried finding the solution in lxml xpath documentation but could not find any mechanism to skip this single quote.
I did find a similar qustion but it does not have any answer.
I also tried replacing single quote with &apos; but didn't work.
Please let me know if there is a way to skip single quote or if I am doing something wrong here.
You can try the below options.
parser.xpath("""./SPLIT/NODE[contains(text(), "node1")]/SPLIT/NODE[contains(text(), "node2")]/SPLIT/NODE[contains(text(), "param's name")]""")
In short
.//NODE[text()="node1/node2/param's name"]

Escaping values for vim.command, vim.eval in Python vim plugins

I'm writing a python plugin for vim and it's looking like the only way to call a specific command is with the vim.command function. However, just substituting values into the function seems like a bad idea. How would I escape values so that I can pass untrusted data as an argument into a vim function? As a simple example, let's say I want to echo out untrusted input (I know I could just use print, but this is just an example). I would do something like:
value = get_data_from_untrusted_source()
vim.command("echo %s" % value)
However, if that untrusted data has a | in it, the command is ended and a new one is executed which is bad. Even if I use quotes, we end up with sql injection like attacks where an attacker can just put an apostrophe in their response to end the string. Then if we double quote, it could be possible to put a backslash somewhere to end the quote. For example if we just double quotes we would go from \' to \'' which escapes the first quote.
Basically what I'm asking is if there's a safe way to call vim functions from a python plugin and would appreciate any help.

Newbie to Python syntax - Error - suspect use of single/double quotes

This is my first time using Python and I am receiving the attached message when attempting to run a script.
here is the code (line 32 is the line starting "font-size:...":
# County style
'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
marker-start:none;stroke-linejoin:bevel;fill:'
Could any helpful person tell me whats up here?
You are using a multiline string, you need triple quotes (''' or """). Example:
'''font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
marker-start:none;stroke-linejoin:bevel;fill:'''

Program error while using python subprocess.call() function

I'm trying to open a program while I'm in a python script using the subprocess.call() function, It opens the program but for some reason the program doesn't allows that and just throw an "Unhandaled exception" error, I know the problem is probably in the program so there may be any other command that will open a program, fill some fields and press "Submit?"
Thanks!
Edit: I've no code to post..
str = 'd:\Softwares\X.exe'
subprocess.call(str)
I've also tried with:
subprocess.call(str,shell=True)
Try calling another program the same way. If the problem persists, the problem is with your code. If it goes away, the problem is with the program.
I think changing to 'D:/Softwares/X.exe' or one of the other string formats will help because the '\' character is the escape character ... used for example to denote a new line '\n'.
It probably works if you use forward-slashes (backslashes are escape symbols in Python). If it doesn't, write the first line like this:
str = r'd:\Softwares\X.exe'
The r tells Python that you are creating a raw string, so it will ignore escape symbols. More information at: https://docs.python.org/2/reference/lexical_analysis.html#string-literals

Categories