Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:07:36+00:00 2026-06-16T10:07:36+00:00

I am trying to scrape URLs from a page that uses JavaScript. Instead of

  • 0

I am trying to scrape URLs from a page that uses JavaScript. Instead of having links on the page, they created onClick events for a number of table rows, whereby, when you click the row, it takes you to the link.

I tried scraping the URLs using Mechanize:

agent = Mechanize.new
page = agent.get(url)

page.links_with(:href => /^http?/).each do |link|
  puts link.href
end

But, looking for links via a HREF reference doesn’t work here, because they’re on the page as part of the onClick event:

<tr onclick="window.open('/someurl');">

Is there a good way to use Mechanize, or some other gem, to parse the code on the page and extract the URLs embedded in the onClick event?

If there’s no good out-of-the-box solution, what might be the best regex to do that? I’m a little new to regex, so not quite able to pull together something on my own yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:07:37+00:00Added an answer on June 16, 2026 at 10:07 am

    You should use a parser. Regex and HTML/XML don’t mix well, because regex are not designed to handle the irregularities HTML and XML documents contain. Very simple tasks might work with a pattern, but you’ll quickly find they are fragile and easily broken when the HTML changes.

    Mechanize for Ruby, uses Nokogiri internally, which is an excellent way to get at those parameters. You can access Mechanize’s internal Nokogiri document and, from it, find the <tr> tags:

    require 'mechanize'
    
    page = Mechanize.new
    page = agent.get('http://somesite.foo.com')
    
    page.search('tr[onclick]').map{ |n| n['onclick'][/\(['"]([^)]+)['"]\)/, 1] }
    

    If I use Nokogiri directly to parse this fragment:

    <tr onclick="window.open('/someurl');">
    

    I can do this:

    require 'nokogiri'
    
    page = Nokogiri::HTML(%[<tr onclick="window.open('/someurl');">])
    page.search('tr[onclick]').map{ |n| n['onclick'][/\(['"]([^)]+)['"]\)/, 1] }
    => ["/someurl"]
    

    Notice that I’m searching using a CSS accessor 'tr[onclick]', which makes it pretty easy to find a particular node. If you know JavaScript, CSS or jQuery you’ll find it pretty easy to pick up Nokogiri using its built-in support for CSS.

    Also,

    n['onclick'][/\(['"]([^)]+)['"]\)/, 1]
    

    could be written alternately as:

    n['onclick'][/\(([^)]+)\)/, 1][1..-2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to scrape all the urls on the home page on my
I am trying to scrape a site that returns its data via Javascript. The
I'm trying to scrape a web app that uses a few redirects and logouts/logins
I'm trying to scrape some information from a web site, but am having trouble
I'm trying to scrape only article text from web pages. I have discovered that
Trying to scrape a Y! Group and I can get data from one page
I'm trying to scrape the courses from ASU's schedule of classes page. I'm doing
I am trying to scrape some content from an HTML page. I'm using libxml2
I'd been trying to scrape some date from as asp.net website, the start page
I'm trying to scrape a price from a web page using PHP and Regexes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.