Home > Enterprise >  How to find or get exact value from python list?
How to find or get exact value from python list?

Time:01-05

suppose I find all scripts using bs4soup

scripts= soup.find_all('script')

got all the script list, now if I need only index no 15 script, starting var numreported = 0;

how do I find or extract that script only?

Though var numreported = 0; script keeps changing the 15 index position to 16, 17 or somewhere.

[<script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-789618-4"></script>, <script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-789618-4');
</script>, <script async="" src="https://www.youtube.com/iframe_api"></script>, <script src="/photoswipe/photoswipe.min.js"></script>, <script src="/photoswipe/photoswipe-ui-default.min.js"></script>, <script async="" defer="" src="//services.bilsyndication.com/adv1/?d=1281" type="text/javascript"></script>, <script>
                var vitag = vitag || {};
                if ('outStreamConfig' in vitag == false ) vitag.outStreamConfig = {};
                vitag.outStreamConfig.enablePC = false;
                vitag.outStreamConfig.enableMobile= false;
                vitag.smartBannerConfig = vitag.smartBannerConfig || {};
                vitag.smartBannerConfig.enablePC = false;
                vitag.smartBannerConfig.enableMobile = false;
                </script>, <script type="text/javascript">
                var infolinks_pid = 3029683;
                var infolinks_wsid = 0;
                </script>, <script src="//resources.infolinks.com/js/infolinks_main.js" type="text/javascript"></script>, <script language="Javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ouibounce/0.0.12/ouibounce.min.js"></script>, <script language="JavaScript" src="/wikifeet.js?ver=10"></script>, <script>
        messanger['gender'] = 1;
        messanger['mbf'] = 1;

    function Get(name)
    {
        return document.getElementById(name);
    }

    function Class(name, cname)
    {
        Get(name).className = cname;
    }


</script>, <script type="application/ld json">{
                                        "@context": "http://schema.org/",
                                        "@type": "CreativeWorkSeries",
                                        "name": "Taylor Bennett's Feet",
                                        "aggregateRating":
                                        {
                                                "@type": "AggregateRating",
                                                "bestRating": "5",
                                                "ratingValue": "3.67",
                                                "ratingCount": "40",
                                                "description": "Rated 3.67/5 stars"
                                  }     }
                                }</script>, <script async="" src="https://static.addtoany.com/menu/page.js"></script>, <script>  
                        //gtag('event', 'Gallery Views', { 'event_label': 'Taylor-Bennett'});
                        messanger.cid  = 8562959;
                        messanger.cfname = 'Taylor-Bennett';
                        messanger.originalRank = 0;
                        messanger.userid = 0;
                        messanger.nickname = '';
                        messanger.profile = 0;
                        messanger.eus = 0;
                        messanger.glevel = 0;
                        messanger.fav = new Array();
                        messanger.fav[8562959] = 0;
                        messanger.pidlist = new Array();
                        </script>, <script>
        var numreported = 0;
        messanger['tagname'] = {"C":"Close-up","A":"Arches","N":"Nylons","B":"Barefoot","T":"Toenails","S":"Soles"};
        messanger['gdata'] = [{"similarity":null,"reported":"0","tags":"","ureported":null,"new":0,"gid":0,"pw":750,"pid":4612500,"ph":1334,"closest":null},{"ph":1080,"pid":4612505,"closest":4612500,"gid":0,"pw":1080,"new":0,"reported":"0","similarity":331,"tags":"","ureported":null},{"ureported":null,"tags":"","reported":"0","similarity":324,"new":0,"pw":1080,"gid":0,"pid":4612506,"closest":4612500,"ph":1080},{"pid":4836419,"ph":1235,"closest":4612500,"gid":0,"pw":1242,"new":0,"reported":"0","similarity":331,"tags":"","ureported":null},{"ureported":null,"similarity":250,"reported":"0","tags":"","new":0,"pw":1242,"gid":0,"pid":4836420,"ph":1253,"closest":4836419},{"new":0,"ureported":null,"similarity":312,"reported":"0","tags":"","pid":4836422,"closest":4836419,"ph":1550,"pw":1242,"gid":0},{"gid":0,"pw":1242,"closest":4836419,"pid":4836423,"ph":1561,"reported":"0","similarity":285,"tags":"","ureported":null,"new":0},{"ureported":null,"reported":"0","similarity":305,"tags":"","new":0,"pw":1099,"gid":0,"pid":5567104,"closest":4836419,"ph":1412},{"similarity":303,"reported":"0","tags":"","ureported":null,"new":0,"gid":0,"pw":1188,"ph":1188,"pid":5910453,"closest":4836419},{"pid":5930819,"ph":2048,"closest":4836419,"pw":1354,"gid":0,"new":0,"ureported":null,"similarity":291,"reported":"0","tags":""},{"pw":588,"gid":0,"pid":6101012,"ph":688,"closest":4836419,"ureported":null,"similarity":273,"tags":"","reported":"0","new":0},{"reported":"0","similarity":235,"tags":"","ureported":null,"new":0,"gid":0,"pw":1080,"ph":713,"pid":6101013,"closest":6101012},{"closest":6101013,"pid":6330671,"ph":1737,"gid":0,"pw":448,"new":0,"similarity":172,"tags":"","reported":"0","ureported":null},{"pid":6330672,"closest":6101013,"ph":1268,"pw":447,"gid":0,"new":0,"ureported":null,"reported":"0","similarity":177,"tags":""},{"ph":1291,"pid":6345943,"closest":6101013,"gid":0,"pw":1080,"new":0,"reported":"0","tags":"","similarity":169,"ureported":null},{"new":0,"tags":"","similarity":170,"reported":"D","ureported":null,"ph":1350,"pid":6381583,"closest":6101013,"gid":0,"pw":1080},{"ureported":null,"similarity":173,"tags":"","reported":"0","new":0,"pw":1717,"gid":0,"ph":1337,"pid":6388872,"closest":6101013},{"new":0,"reported":"0","tags":"","similarity":165,"ureported":null,"ph":1350,"pid":6528752,"closest":6101013,"gid":0,"pw":1080},{"ph":700,"pid":6543593,"closest":6101013,"pw":613,"gid":0,"new":0,"ureported":null,"similarity":168,"reported":"0","tags":""},{"closest":6101013,"pid":6543594,"ph":802,"gid":0,"pw":720,"new":0,"reported":"0","similarity":171,"tags":"","ureported":null},{"pid":6577840,"ph":1350,"closest":6101013,"gid":0,"pw":1080,"new":0,"reported":"0","similarity":157,"tags":"","ureported":null},{"pid":6582034,"closest":6101013,"ph":720,"pw":1080,"gid":0,"new":0,"ureported":null,"similarity":157,"reported":"0","tags":""},{"new":0,"ureported":null,"reported":"0","similarity":169,"tags":"","closest":6101013,"pid":6619826,"ph":1799,"pw":1440,"gid":0},{"ureported":null,"reported":"0","similarity":166,"tags":"","new":1,"pw":1440,"gid":0,"closest":6101013,"pid":6939532,"ph":1777},{"pid":6939533,"ph":1782,"closest":6101013,"gid":0,"pw":1440,"new":1,"similarity":166,"reported":"0","tags":"","ureported":null}];
        for (var i in messanger['gdata'])
        {
                messanger.pexist[messanger['gdata'][i].pid] = 1;
                if (messanger['gdata'][i].reported != '0') numreported  ;
        }

                var hash = window.location.hash.substr(1);
                if (hash == 'reports') document.getElementById('gdatasort').value = 'rep';
        if (getCookie('nophotos') == 'true') {}
        else pDisplayPage(0, 1);
        </script>, <script>(vitag.Init = window.vitag.Init || []).push(function(){viAPItag.display("vi_12813030")})</script>, <script>
                        messanger['scrollfunc'] = LoadOlderComments;
                        window.onscroll = function() { messanger['scrollfunc'](); }
                </script>, <script>messanger['lastcomment'] = '';</script>, <script crossorigin="anonymous" integrity="sha512-yXXqOFjdjHNH1GND 1EO0jbvvebABpzGKD66djnUfiKlYME5HGMUJHoCaeE4D5PTG2YsSJf6dwqyUUvQvS0vaA==" src="https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.1/cookieconsent.min.js"></script>, <script>
        window.addEventListener("load", function(){
        window.cookieconsent.initialise({
          "palette": {
            "popup": {
              "background": "#000"
            },
            "button": {
              "background": "#f1d600"
            }
          }
        })});
        </script>]

CodePudding user response:

scripts = soup.find_all('script')
for script in scripts:
    if 'var numreported = 0;' in script.getText().lower():
        print(script.getText())

CodePudding user response:

you can use this method to find what you want :

import re
def find(t1, t2) :
  ma =re.search(t1,t2)
  if ma :
    return ma.group() 
  else:
    print ('not found')

now you can use it :

scripts= soup.find_all('script')
t2 = str(scripts) 
result = find (r'<script>\.*var numreported = 0\.*</script>', t2)

but I dont know if scripts here is normal python list or some kind of cursor or somthing else, if scripts is python list it must work, if it is not list you have to find way to make it string first!

  • Related