Selecting id attribute using xpath/scrapy - xpath

I am trying to select the user name from the following forum url.
However, when I use the following in the scrapy shell:
admin:~/workspace/scrapper (master) $ scrapy shell "https://bitcointalk.org/index.php?action=profile;u=22232"
In [1]: response.xpath('//*[#id='bodyarea']/table/tbody/tr/td/table/tbody/tr[2]/td[1]/table/tbody/tr[1]/td[2]')
File "<ipython-input-4-abe70514018b>", line 1
response.xpath('//*[#id='bodyarea']/table/tbody/tr/td/table/tbody/tr[2]/td[1]/table/tbody/tr[1]/td[2]')
^
SyntaxError: invalid syntax
However, in Chrome the selector works fine.
Any suggestions what I am doing wrong?
I appreciate your replies!

This is because of quotes inconsistent usage. Note that you're using single quotes both for XPath and string inside XPath.
Use either
'//*[#id="bodyarea"]/table...'
or
"//*[#id='bodyarea']/table..."

Related

How to escape a doublequote-sign inside a (tau) prolog string?

I guess there must be an easy answer to this, but I just haven't been able to find it -
I want to include double-quote signs inside tau-prolog strings.. how to do it?
When I try entering it into the Tau-Prolog sandbox (http://tau-prolog.org/sandbox/) I get the following interaction:
A = "hello there, \"world\"".
error parsing query: error(syntax_error(unknown_escape_sequence),[line(1),column(4),found('"hello there, \\"')])
Any ideas?
Where did I (stupidly) misunderstand things? :)
PS - it's not only a problem in the sandbox - if try to run a Tau-Prolog program (in the browser), and use a double-quote character anywhere inside a string I get the same parsing-problem.

How can I update column values with the content of a file without interpreting it?

I need to update values in a column when the row matches a certain WHERE clause, using the content of a text file.
The content of the file is javascript code and as such it may contain single quotes, double quotes, slashes and backslashes - out of the top of my mind, it could contain other special characters.
The content of the file cannot be modified.
This has to be done via psql, since the update is automated using bash scripts.
Using the following command - where scriptName is a previously declared bash variable -
psql -U postgres db<<EOF
\set script $(cat $scriptName.js))
UPDATE table SET scriptColumn=:script WHERE nameColumn='$scriptName';
EOF
returns the following error
ERROR: syntax error at or near "{"
LINE 1: ...{//...
^
I would like to treat the content of the file $scriptName.js as plain text, and avoid any interpretation of it.
You should quote the variable:
UPDATE table SET scriptColumn=:'script' WHERE ...
That causes the contents of the variable to be properly escaped as a string literal.
I found a solution to my problem, even though I don't know why it works.
I leave it here in the hope it might be useful to someone else, or that someone more knowledgeable than me will be able to explain why it works now.
In short, setting the variable as a psql parameter did the trick:
psql -U postgres db -v script="$(cat $scriptName.js)"<<EOF
UPDATE table SET scriptColumn=:'script' WHERE nameColumn='$scriptName'
EOF
Not sure how this differs from
psql -U postgres db <<EOF
\set script "$(cat $scriptName.js)"
UPDATE table SET scriptColumn=:'script' WHERE nameColumn='$scriptName'
EOF
which I tried previously and returns the following error:
unterminated quoted string
ERROR: syntax error at or near "//"
LINE 1: // dummy text blahblah
Thanks to everybody who helped!

Scrapy xpath scraping meta

I'm scraping with scrapy this url: http://quotes.toscrape.com/
it works great when I do:
response.xpath("//meta[#itemprop='keywords']/#content").extract()
response.xpath("//meta[#itemprop='keywords'][1]/#content").extract_first()
but when I try to get the second meta from that list of metas using the index
response.xpath("//meta[#itemprop='keywords'][2]/#content").extract_first()
it doesn't work.
What am I missing?
Thanks!
You need to wrap the expression before index in parenthesis:
Instead of:
"//meta[#itemprop='keywords'][2]/#content"
It should be:
"(//meta[#itemprop='keywords'])[2]/#content"
This is needed because you have parameter operators in your xpath.
You can test this:
$ scrapy shell "http://quotes.toscrape.com/"
In [1]: response.xpath("//meta[#itemprop='keywords'][2]/#content").extract_first()
In [2]: response.xpath("(//meta[#itemprop='keywords'])[2]/#content").extract_first()
Out[2]: 'abilities,choices'

bash: syntax error near unexpected token `(' - PIG, CentOs

I am trying to execute the following command in pig
7369,SMITH,CLERK,800.00,null,20
7499,ALLEN,SALESMAN,1600.00,300.00,30
Script
emp_bag = LOAD '/home/training/dvs/emp.csv' using PigStorage(',') AS (eno:int, ename:chararray, job:chararray, sal:int, comm:int, deptno:int);
And getting the below error
bash: syntax error near unexpected token `('
Please help to resolve this.
Are you running your pig command on bash ?
If yes, please start the pig console first and then run it.
Just type pig and enter.
Most likely the issue is the data of type float.You need to change the datatype for 4th and 5th field to float from int.
Also if null is a string then you will have to handle it using chararray field and replace 'null' with ''.
emp_bag = LOAD '/home/training/dvs/emp.csv' using PigStorage(',') AS (eno:int, ename:chararray, job:chararray, sal:float, comm:float, deptno:int);
Alternatively,you can check whether the issue is with the datatype by not specifying the schema in which case the default datatype will be bytearray.
emp_bag = LOAD '/home/training/dvs/emp.csv' using PigStorage(',')
You probably haven't activated the grunt shell.

Why doesn't 'text{$variable}' work for me?

I'm using the latest stable version of Smarty and can't get this string to work. I've looked at other questions for solution to do this but none seem to work.
This is a template file (TPL), and doesn't contain any PHP at all. Note that the TPL file is compiled to a PHP script and then sent to the browser. It's not a PHP file.
Current code:
'foo{$bar}'
which outputs as:
'foo{$bar}'
instead of the value of $bar.
What am I doing wrong?
If you use any variables within text you have to use double-quotes " instead of single quotation marks '. Text within single quotation marks is not parsed for variables in PHP.

Resources