I am trying to find a specific variable definition inside a site's HTML (in a tag) I have the following code:
logResponse = scrape.post(url, params=logindata, headers=UA)
soup = bs(logResponse.text, 'html.parser')
x = soup.find_all('var my_post_key')
print(x)
The variable I am looking for is "my_post_key", but the soup.find_all function returns an empty list ([]). I suspect I am using it wrong, but am wondering how one would do this properly. This is how the variable is stored in page's HTML:
<script type="xxxxxxxxxxxxxxxxx-text/javascript">
var my_post_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
</script>
To recap, I am just trying to fetch the "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" value. Any help is appreciated.
find_all()looks for tags, and "var my_post_key" isn't a tag. Usefind_all('script')instead, then search through those results.findandselect_onewill return only the first foundtag, whilefindAllandselectwill return aResultSet, and then you can parse theelementusing.textor.string, so you can split then according to your desired, something likesplit('"')[1]or usingregex