1

I need to follow the first result of a search on a website.

I open the website by feeding the Name that I am looking for from a .csv file so that it opens the website with the search already performed.

def name_to_url(name):
    words = name.split(" ")
    url = "http://website/search/results?Name="
    end_of_url = "&Type=0&IncludeNlsp=True"
    for word in words:
        url += "%s+" % word
    url += "%s" % end_of_url

    return url

with open('file.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        open_page(name_to_url(row[0]))

I know this may not be the prettiest or best way to do it but it is good enough for now. My main concern is how to follow the link that the search returns.

Let's say the name would be "Google" and the search returns a link with bold green text that reads "Google". I've had a look at mechanize but I can't figure out how to do it, mainly because the example on the website uses regular expressions

Sebastian
  • 141
  • 3
  • 13

1 Answers1

0

There are multiple ways to extract the link and follow it with mechanize. The easiest option that might work for you, would be to get it by index using:

browser.follow_link(nr=number)

Or, you can use browser.links() to filter out links by url_regex or text_regex.

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195