简体   繁体   中英

Python lxml xpath unable to get text

I want to get the symbol and company from this url "https://www.set.or.th/set/commonslookup.do?language=en&country=US&prefix=A" However, it returns nothing and the response is [200]

print "hello from python 2"
from lxml import html
import requests
import csv
import pandas as pd

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
page = requests.get('https://www.set.or.th/set/commonslookup.do?language=en&country=US&prefix=A', headers=headers)
tree = html.fromstring(page.content)
tree1 = tree.xpath('//td/text()')
tree2 = tree.xpath('//td/a/text()')
print tree1
print tree2

How can I get the text for all symbol and company?

The entire page contents is loaded via jquery. If you look at the content in your response, you will see there is very little other than a wrapper around a javascript call that that dynamically loads the page content.

page.content
# returns:
b'<html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection"
content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=
edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe id="main-iframe" src="/_Incapsula_Resource?SWUDNSAI=3
0&xinfo=0-25937873-0%200NNN%20RT%281572429155601%20933%29%20q%280%20-1%20-1%200%29%20r%280%20-1%29%20B12%284%2c315%2c0%2
9%20U5&incident_id=476000980067714022-125254320263005728&edet=12&cinfo=04000000&rpinfo=0" frameborder=0 width="100%" hei
ght="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 476000980067714022-12525432
0263005728</iframe></body></html>'

Unfortunately, this means you will need to use a library that supports content loaded this way. Either Selenium with PhantomJS or you can try to use requests_html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM