2

I tried whole day but unable to extract the overwritten texts of p tag. For example:

<p id='demo'><p>
<Script>
  Var = document.getElementById('demo').innerHTML = 'some context'
<Script>

I need the text 'some context' into python. I tried with beautifulsoup

page = soup.find('p').getText()

I'm getting nothing. Is there any way to get changed text of p tag. Any Help will be appreciated.

matszwecja
  • 6,357
  • 2
  • 10
  • 17
Niya
  • 41
  • 3
  • refer this link : https://stackoverflow.com/a/3987802/8133524 i hope it will work – Ramesh Jul 04 '22 at 10:56
  • 1
    You will need to actually run the javascript before the 'demo' element gets "some context" inside it. – qrsngky Jul 04 '22 at 10:58
  • 1
    `beautifulsoup` don't execute javascript. You have to do request using `Selenium` or another headless browser to be able render html page – rzlvmp Jul 04 '22 at 11:06
  • Thanks all, @qrsngky running javascript before 'demo' element also giving me nothing. – Niya Jul 04 '22 at 11:33
  • @rzlvmp is right: you can't run javascript just using beautifulsoup. Wherever you put the script tag, it's not executed. – qrsngky Jul 04 '22 at 11:38
  • @rzlvmp I am able to render html page with all changed text made in a p tags using JS, only unable to get that changed text to python – Niya Jul 04 '22 at 11:39
  • By "render", you mean in a browser? And how do you run the Python part? – qrsngky Jul 04 '22 at 11:40
  • yes, It rendered in browser, its showing there. I need that text into backend now. @qrsngky – Niya Jul 04 '22 at 11:43
  • 2
    `yes, It rendered in browser` → `beautifulsoup` is not a browser. Check for `Selenium` – rzlvmp Jul 04 '22 at 11:48

0 Answers0