Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
How to Parse javascript content with BeautifulSoup

I'm struggling to parse some var in my HTML

Here is the example of HTML:

<script type="text/javascript">
        var ASPath = "\/modules\/pm_advancedsearch4\/";
        var ASSearchUrl = "https:\/\/golf-land.fr\/module\/pm_advancedsearch4\/advancedsearch4";
        var as4_orderBySalesAsc = "Meilleures ventes en dernier";
        var as4_orderBySalesDesc = "Meilleures ventes en premier";
        var controller = "my-account";
</script>

I have tried doing this:

soup = BeautifulSoup(s.text, 'html5lib')

soup = BeautifulSoup(str(soup.find_all('script')[2]), "html.parser")

pattern = re.compile(r"var controller = '(.*?)';$", re.MULTILINE | re.DOTALL)
print(pattern)
script = soup.find("script", text=pattern)

print(pattern.search(script.text).group(1))

My aim was to get the "my-account" but all I got was

re.compile("var controller = '(.*?)';$", re.MULTILINE|re.DOTALL)
Traceback (most recent call last):
  File "main.py", line 47, in <module>
    print(pattern.search(script.text).group(1))
AttributeError: 'NoneType' object has no attribute 'text'

Line 47 refers to the last line of my code.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are confusing as to what it is to use bs4 to get specific tags, and to parse out a substring from that content.

The pattern you are searching for is looking for an exact match, while what you want is to find that content that contains the substring (to get the specific tag).

So your regex pattern should be pattern = re.compile(r"var controller = (.*?);$", re.MULTILINE | re.DOTALL) to get that <script> tag as a BeautifulSoup object.

Also, your pattern is looking for single ' quotes, when there are none in "my-account".

But really what you are wanting is to pull out that substring after you got the specific tag.

Try this:

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup(s.text, 'html5lib')
script = soup.find_all('script')[2]

pattern = re.compile(r"var controller = (.*?);$", re.MULTILINE | re.DOTALL)
#print(pattern)
scriptParse = re.search(pattern, str(script))

print(scriptParse.groups(1)[0])

Output:

"my-account"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!