1

I was wondering if someone can help me with being able to grab/scrape/retrieve an email address from a web page.

I want to create an online petition/contact form, whereby the user can enter their "Post Code" and it will automatically retrieve the email address of their MP.

Add it to the template email I have written and send it to them.

So, I was able to discover the search URL through inspecting the header which is this one: http://www.parliament.uk/mps-lords-and-offices/mps/?search_term=m224fh

The "m224fh" here is the Post Code.

This will then redirect you to the page for your local MP, where one can find their contact details including their email address.

The email is located in the following html tags and elements:

    <div id="pnlCommonsAddressDetails">
          <div class="contact-detail parliamentary">
              <p> ... </p>
              <p> ... </p>
              <p id="ctl00_ctl00_FormContent_SiteSpecificPlaceholder_PageContent_addParliamentaryAddress_rptAddresses_ctl00_pnlEmail" data-generic-id="email-address"> Email: <a id="ctl00_ctl00_FormContent_SiteSpecificPlaceholder_PageContent_addParliamentaryAddress_rptAddresses_ctl00_hypEmailAddress" href="mailto:this.email.address@parliament.uk">this.email.address@parliament.uk</a></p>

So from the example above, I want to be able to grab/scrape

this.email.address@parliament.uk

and then echo it in my php script to enable the user to email their local MP with the template letter/email I have written.

Thank you very much for the help and guidance in advance.

  • Which part are you having trouble with? [Loading the text from a URL](https://stackoverflow.com/questions/3249157/php-how-can-i-load-the-content-of-a-web-page-into-a-variable), [parsing HTML](https://secure.php.net/manual/en/domdocument.loadhtml.php), or [traversing the DOM](https://secure.php.net/manual/en/domdocument.getelementsbytagname.php)? – Ken Y-N Oct 04 '17 at 03:44
  • Loading the text from a URL and then parsing the HTML. Because the URL is not the actual page, the URL will return a page, and then the email have to be parsed from that page. –  Oct 04 '17 at 10:07

1 Answers1

1

You can use

var el = $('[data-generic-id="email-address"]')[0]

to fetch the paragraph with email address. Then use $(el).html() to fetch the content (email address) inside.

Tapas
  • 1,123
  • 8
  • 16