views:

318

answers:

3

Hi,

I want to use mechanize with python to get all the links of the page, and then open the links.How can I do it?

A: 

The Browser object in mechanize has a links method that will retrieve all the links on the page.

Hank Gay
+2  A: 

Here is an example from the project's page:


import re
from mechanize import Browser

br = Browser()
br.open("http://www.example.com/")

# ...

# .links() optionally accepts the keyword args of .follow_/.find_link()
for link in br.links(url_regex="python.org"):
    print link
    br.follow_link(link)  # takes EITHER Link instance OR keyword args
    br.back()
Geo
A: 

---Rules Hi,

I have found all the anchor tags of page using python and from these anchor tags i have again found the associated anchor tags i've done this till nth level. now i want to create a tree structure so that i can differentiate between parent and child node.

ex. I've the structure in d drive as follows:

Page.html---School.html--Principal--Rules --teacher ---Office.html---Rules

I've fetched :

d://PythonSample/Page.html d://PythonSample/School.html d://PythonSample/principal.html d://PythonSample/Rules.html d://PythonSample/teacher.html d://PythonSample/Rules.html d://PythonSample/Office.html d://PythonSample/Rules.html

now i wish to create the above tree structure using python. Kindly suggest some help.

Neha
If you have a question, please **Ask Question**. Don't post it as an answer.
SilentGhost