Python Forum

Full Version: Saving links as text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

pages=set()
def getLinks(pageUrl):
    global pages
    html=urlopen("https://heppa.hippos.fi"+pageUrl)
    bsobj=BeautifulSoup(html, 'lxml')
    for link in bsobj.findAll("a", href=re.compile("^(/heppa/)")):
        if 'href' in link.attrs:
            if link.attrs['href'] not in pages:
                newPage=link.attrs['href']
                print(newPage)
                pages.add(newPage)
                getLinks(newPage)
getLinks("")



I'm new in Python and web scraping. I found this code somewhere. Trying modify code so I can save links to file, but I cant.

Please help me.
Thanks in advance.
Sad
This code may have worked in the past, (and still may) saving should be simple, but...
the webpage is almost entirely javaScript, so to properly scrape you should use selenium.
there are two tutorials you on this site you should run through (doesn't take long):
web scraping part1
web scraping part2