ImportError : BeautifulSoup이라는 모듈이 없습니다. ‘<p id=”secondpara” align=”blah”>This

easy_install을 사용하여 BeautifulSoup을 설치하고 다음 스크립트를 실행하려고합니다.

from BeautifulSoup import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

하지만 왜 이런 일이 일어나는지 잘 모르겠습니다.

Traceback (most recent call last):
  File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module>
    from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup

도와 주 시겠어요? 감사



답변

이 시도 from bs4 import BeautifulSoup

뷰티플 수프 버전 4, 베타 버전의 문제 일 수 있습니다. 방금 홈페이지에서 읽었습니다.


답변

Ubuntu 14.04에서는 apt-get에서 설치했으며 제대로 작동했습니다.

sudo apt-get install python-beautifulsoup

그런 다음 다음을 수행하십시오.

from BeautifulSoup import BeautifulSoup


답변

이것을 시도해보십시오, Mine은 이런 식으로 작동했습니다. 태그의 데이터를 얻으려면 “a”를 원하는 태그로 바꾸십시오.

from bs4 import BeautifulSoup as bs
import urllib

url="http://currentaffairs.gktoday.in/month/current-affairs-january-2015"

soup = bs(urllib.urlopen(url))
for link in soup.findAll('a'):
        print link.string

답변

BeautifulSoup 대신 bs4를 가져올 수 있습니다. bs4는 내장 모듈이므로 추가 설치가 필요하지 않습니다.

from bs4 import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

요청하려면 requests 모듈을 사용하십시오. 요청이 urllib, requests모듈을 사용 하고 있습니다. 하지만 개인적으로 requests모듈 대신urllib

사용을위한 모듈 설치 :

$ pip install requests

요청 모듈을 사용하는 방법은 다음과 같습니다.

import requests as rq
res = rq.get('http://www.example.com')

print(res.content)
print(res.status_code)

답변

먼저 아름다운 수프 버전 4를 설치하십시오. 터미널 창에 명령을 작성하십시오.

pip install beautifulsoup4

그런 다음 BeutifulSoup 라이브러리를 가져옵니다.


답변

두 가지 버전의 파이썬이 있다면 내 상황이 도움이 될 수 있습니다.

이것이 내 상황이다

1-> 맥 OSX

2-> 나는 두 가지 버전의 python, (1) 시스템 기본 버전 2.7 (2) 수동으로 버전 3.6을 설치했습니다.

3-> 나는 beautifulsoup4를 설치했다. sudo pip install beautifulsoup4

4-> 나는 파이썬 파일을 다음과 같이 실행합니다. python3 /XXX/XX/XX.py

따라서이 상황 3과 4가 핵심 부분입니다. “pip”로 beautifulsoup4를 설치했지만이 모듈은 python verison 2.7 용으로 설치되었으며 “python3″으로 python 파일을 실행합니다. 그래서 파이썬 3.6 용으로 beautifulsoup4를 설치해야합니다;

와 함께 sudo pip3 install beautifulsoup4파이썬 3.6 용 모듈을 설치할 수 있습니다.


답변

이 방법으로 설치 한 경우 (아니면이 방법으로 설치) :

pip install beautifulsoup4

이 코드를 사용한 경우 (아니면이 코드를 사용하십시오) :

from bs4 import BeautifulSoup

Windows 시스템을 사용하는 경우 모듈이 있는지 확인하고 모듈의 다른 경로를 저장할 수 있습니다.