Click here to Skip to main content
14,869,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to scrape a page to get tag script only using python. This is the code:
Python
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
import re
import numpy as np

url = urlopen("http:exampleofurl")  
#example of url only
bsj = soup(url.read())
bsj.findAll('script')

This is example of the output:
<pre lang="Python">[<script>
document.documentElement.className = document.documentElement.className.replace( /(\s|$)/, "$1j" );
</script>,
 <script>
         var rtlLs = ['ar','yi'],
             translationsHash = '3a',
             wm = {
                 ready: false,
                 make: function(){
                     if ( !wm.ready ) {
                         wm.ready = true;
                         document.body.className += ' jsl';
                     }
                 }
             };
             window.setTimeout( wm.make, 100 ) </script>,
         <script src="portal/ex.org/assets/js/index.js"></script>,
         <script src="portal/ex.org/assets/js/gt-9.js"></script>]

But i want to get the contents of the script tags only, this is the output that i want:
document.documentElement.className = document.documentElement.className.replace( /(\s|$)/, "$1j" );
     var rtlLs = ['ar','yi'],
         translationsHash = '3a',
         wm = {
             ready: false,
             make: function(){
                 if ( !wm.ready ) {
                     wm.ready = true;
                     document.body.className += ' jsl';
                 }
             }
         };
         window.setTimeout( wm.make, 100 )
     src="portal/ex.org/assets/js/index.js"
     src="portal/ex.org/assets/js/gt-9.js"

There is no script tag there. What should i do to get the output?

And i want to save the output to a javascript file format.

How to save the output to a javascript file format?

What I have tried:

I tried to get script tags using python code, not the contents of the script tags only.
Posted
Updated 21-Mar-21 0:39am

1 solution

   
Comments
gitprb 21-Mar-21 21:21pm
   
thanks for your solution, it's worked. But now how can I save the output to a javascript file format?
Richard MacCutchan 22-Mar-21 4:43am
   
There is no such thing as "Javascript file format". The data is simple text so just write it to a text file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900