0

It shows me error in this line:

name = sel.xpath("//*[@class = "inline t-24 t-black t-normal break-words"]/text()").extract_first().split()

Error:

File "<ipython-input-9-f8d78586cc1a>", line 7
    name = sel.xpath("//*[@class = "inline t-24 t-black t-normal break-words"]/text()").extract_first().split()
                                         ^
SyntaxError: invalid syntax
ernest_k
  • 44,416
  • 5
  • 53
  • 99
levis
  • 43
  • 1
  • 8
  • You have unescaped double quote characters in a double-quoted string. Either use single quotes for the outer string, or escape the inner double quotes. – ernest_k Mar 18 '21 at 08:40

1 Answers1

2

Try this instead:

name = sel.xpath('//*[@class = "inline t-24 t-black t-normal break-words"]/text()1).extract_first().split()

More generally speaking, you may define strings in python using single quotes or double quotes:

str1 = "hi there"
str2 = 'goodbye'

However, if you with to define a string with single / double quotes inside, you need to use the other kind outside for string definition:

str3 = "he said: `I quote with single quotes`"
str4 = 'she said: "I quote with double quotes"'

print(str3) # yields he said: `I quote with single quotes`
print(str4) # yields she said: "I quote with double quotes"

You need to alternate single and double quotes for the string to remain consistent.

Shir
  • 1,571
  • 2
  • 9
  • 27
  • This will change the value of the text itself, which would introduce a problem. Maybe they should use single quotes outside. – ernest_k Mar 18 '21 at 08:49
  • Shir, it worked, but how to identify where we should use single quotes or double quotes? – levis Mar 18 '21 at 08:55
  • I edited the solution to be more informative. Hope this answers your question. – Shir Mar 18 '21 at 08:59
  • Shir, but the O/P of str1 and str2 were same, what's the difference? – levis Mar 18 '21 at 09:06
  • I'm not sure I understand your question, but [this post](https://stackoverflow.com/questions/4630465/how-to-include-a-quote-in-a-raw-python-string) seem to discuss a similar problem. Let me know if you have more questions. – Shir Mar 18 '21 at 09:26
  • Shir thanks a lot, i dont have any qstn right now – levis Mar 18 '21 at 11:01