xpath and css as input in python

0 magic love · July 27, 2015

from lxml import html
import requests
# you can use any website
page = requests.get('http://www.armagsolutions.in/')
tree = html.fromstring(page.text)
#This will create a list of content:
content = tree.xpath('//*[@id="service"]/div//div/h3/text()')
print 'Content: ', content

this works fine,I am getting output,but I want to input a xpath or css selector to grap different portion on same page.

xpath_first = raw_input("> ")

content = tree.xpath(xpath_first)

When used above code this it's not working. When I give same xpath as input [//*[@id="service"]/div//div/h3/text()] but Its return []
I want to give xpath or css selector as an input can anyone explain with example? 

Post a Reply


Oldest  Newest  Rating
0 Halcyon Abraham Ramirez · July 29, 2015
why put it in brackets?

it'll make it a list and tree.xpath expects a string not a list.

and btw switch to python 3 no more raw_input or input confusion

it'll just be plain input 
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator