import re
import mechanicalsoup

# Connect to Google
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.google.com/")

# Fill-in the form
browser.select_form('form[action="/search"]')
browser["q"] = "MechanicalSoup"
browser.submit_selected(btnName="btnG")

# Display links
for link in browser.links():
    target = link.attrs['href']
    # Filter-out unrelated links and extract actual URL from Google's
    # click-tracking.
    if (target.startswith('/url?') and not
            target.startswith("/url?q=http://webcache.googleusercontent.com")):
        target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target)
        print(target)

import re
import mechanicalsoup

# Connect to Google
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.google.com/")

# Fill-in the form
browser.select_form('form[action="/search"]')
browser["q"] = "MechanicalSoup"
# Note: the button name is btnK in the content served to actual
# browsers, but btnG for bots.
browser.submit_selected(btnName="btnG")

# Display links
for link in browser.links():
    target = link.attrs['href']
    # Filter-out unrelated links and extract actual URL from Google's
    # click-tracking.
    if (target.startswith('/url?') and not
            target.startswith("/url?q=http://webcache.googleusercontent.com")):
        target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target)
        print(target)

Add comment about button name on google example
Add comment about button name on google example

  import re
  import mechanicalsoup
  
  # Connect to Google
  browser = mechanicalsoup.StatefulBrowser()
  browser.open("https://www.google.com/")
  
  # Fill-in the form
  browser.select_form('form[action="/search"]')
  browser["q"] = "MechanicalSoup"
+ # Note: the button name is btnK in the content served to actual
+ # browsers, but btnG for bots.
  browser.submit_selected(btnName="btnG")
  
  # Display links
  for link in browser.links():
      target = link.attrs['href']
      # Filter-out unrelated links and extract actual URL from Google's
      # click-tracking.
      if (target.startswith('/url?') and not
              target.startswith("/url?q=http://webcache.googleusercontent.com")):
          target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target)
          print(target)
  
Add comment about button name on google example
## Code Before:
import re
import mechanicalsoup

# Connect to Google
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.google.com/")

# Fill-in the form
browser.select_form('form[action="/search"]')
browser["q"] = "MechanicalSoup"
browser.submit_selected(btnName="btnG")

# Display links
for link in browser.links():
    target = link.attrs['href']
    # Filter-out unrelated links and extract actual URL from Google's
    # click-tracking.
    if (target.startswith('/url?') and not
            target.startswith("/url?q=http://webcache.googleusercontent.com")):
        target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target)
        print(target)

## Instruction:
Add comment about button name on google example
## Code After:
import re
import mechanicalsoup

# Connect to Google
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://www.google.com/")

# Fill-in the form
browser.select_form('form[action="/search"]')
browser["q"] = "MechanicalSoup"
# Note: the button name is btnK in the content served to actual
# browsers, but btnG for bots.
browser.submit_selected(btnName="btnG")

# Display links
for link in browser.links():
    target = link.attrs['href']
    # Filter-out unrelated links and extract actual URL from Google's
    # click-tracking.
    if (target.startswith('/url?') and not
            target.startswith("/url?q=http://webcache.googleusercontent.com")):
        target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target)
        print(target)
