Issue
I want to get href url from onclick
javascript function.
This is my button element:
<button class="module_bnt" onclick="window.location.href='https://someurl.org/module/'"> Click Here to Start Quiz</button>`
This is my parsing function:
def parse(self, response):
articles = response.xpath('//article')
for article in articles:
id = article.xpath('./@id').get()
if id is not None:
id = id.encode('utf-8') # converting to utf-8 and removing u character space in string
moduleBnt = article.xpath('.//button[@class="module_bnt"]/a/@href').get()
if moduleBnt is None:
moduleBnt = article.xpath(".//button[@class="module_bnt"]/a/@onclick").extract_first()
Can anyone help me on this?
Solution
You can get the onclick
attribute value using css selector and then use regex to extract the url.
Something like this:
url = response.css('.module_bnt::attr(onclick)').re('href='(.*)')[0]
Answered By - asimhashmi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.