Extract all links from a web page using python

前端 未结 3 560
死守一世寂寞
死守一世寂寞 2020-12-28 11:03

Following Introduction to Computer Science track at Udacity, I\'m trying to make a python script to extract links from page, below is the code I used:

I got the fol

3条回答
  •  独厮守ぢ
    2020-12-28 11:37

    page is undefined and that is the cause of error.

    For web scraping like this, you can simply use beautifulSoup:

    from bs4 import BeautifulSoup, SoupStrainer
    import requests
    
    url = "http://stackoverflow.com/"
    
    page = requests.get(url)    
    data = page.text
    soup = BeautifulSoup(data)
    
    for link in soup.find_all('a'):
        print(link.get('href'))
    

提交回复
热议问题