0

I am writing some syntax to parse website and get all the href there. However, when I try to import bs4, it pops out an error saying "ImportError: cannot import name 'HTMLParseError'. I am using Python 3.5.2.

I take the past reference and know that it may be due to the old version of bs4 and hence has upgraded that to version 4.5.1. However, the error still exists. Is that something wrong with my syntax (I attached below, which is also from past reference). Or I have to seek another tool for doing the task?

Could anyone has any idea? One more thing, I also try to install lxml (it said unable to find vcvarsall.bat) but failed too. So, not many tools I can use.

from bs4 import BeautifulSoup
import urllib.request

def open_html():

    resp = urllib.request.urlopen("http://www.gpsbasecamp.com/national-parks")
    soup = BeautifulSoup(resp, from_encoding=resp.info().get_param('charset'))

    for link in soup.find_all('a', href=True):
        print(link['href'])

if __name__ == '__main__':
    open_html()
CL. L
  • 237
  • 1
  • 7
  • 22

2 Answers2

0

If you want to install lxml manually, you can download lxml .whl file copiled from page http://www.lfd.uci.edu/~gohlke/pythonlibs/. Next open cmd, cd to dict where you saved this file, and use command:

pip install [name_of_file]

But that's simplest way to remove this problem, for else I refer you to topic:

pip install gives error: Unable to find vcvarsall.bat

Community
  • 1
  • 1
dannyxn
  • 422
  • 4
  • 16
0

As an alternative measure, install Anaconda python, which includes BS 4.4.1 and lxml 3.6 (https://docs.continuum.io/anaconda/pkg-docs) already. And in general, Anaconda makes package management easy like a breeze.

dgg32
  • 1,409
  • 1
  • 13
  • 33
  • Thanks for the comments. Is Anaconda portable? I may code python on a machine without admin right, so can't install it. I am now using WinPython which allows me not to register on the machine for running. – CL. L Aug 23 '16 at 02:54
  • Actually, would this error be caused by defect in or absence of Visual Studio? – CL. L Aug 23 '16 at 02:55
  • I installed it multiple times on accounts without admin privilege, on Linux and Mac machines alike. It is out of the box and runs without any problem. So I guess VS is not a prerequisite, but I haven't tried anything on Windows for ages. – dgg32 Aug 23 '16 at 21:53
  • Ok, thanks for he comments, will try later. Back to the bs4, I guess it's the problem about VS as I tried Winpython with bs version 4.4.1 on another computer, it works. So may be it's due to the defect of VS happened in installation. – CL. L Aug 24 '16 at 00:59
  • I just installed the Anaconda on my machine, seem everything works well there, and easier than Winpython in terms of navigation and package installation. I think I will switch to using this platform. Thanks for your suggestions – CL. L Aug 26 '16 at 03:03