在"urlib“库下,无论我们有‘request’模块还是'request'模块
运行以下代码时
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print (bsObj.h1)shell正在抛出警告
Warning (from warnings module):
File "C:\Python34\lib\site-packages\bs4\__init__.py", line 181
markup_type=markup_type))
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 1 of the file <string>. To get rid of this warning, change code that looks like this:
BeautifulSoup(YOUR_MARKUP})
to this:
BeautifulSoup(YOUR_MARKUP, "html.parser")**
<h1>An Interesting Title</h1>当我这么做的时候
>>>import requests哪个shell成功导入请求模块
但是当我将上面的代码更改为
from urllib.requests import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print (bsObj.h1)shell抛出错误消息。
Traceback (most recent call last):
File "C:\Python34\scrapetest.py", line 1, in <module>
from urllib.requests import urlopen
ImportError: No module named 'urllib.requests'而pip工具成功地安装了两个模块(请求,请求)。
C:Python34\Scripts>pip install request
Requirement already satisfied <use --upgrade to upgrade>: request in c:\python34\lib\site-packages
cleaning up...
C:Python34\Scripts>pip install requests
Requirement already satisfied <use --upgrade to upgrade>: requests in c:\python34\lib\site-packages
cleaning up...发布于 2017-12-29 07:02:30
urllib是它拥有的一个模块,对于requests来说也是一样的。你做了:
import requests
from urllib.request import urlopen它是urllib.request而不是urllib.requests,它解释了python解释器给您带来的错误。这是文档 for urllib
至于您正在获得的第一个shell错误,python已经提示您如何解决它。因此,与其:
bsObj = BeautifulSoup(html.read())你应该这么做:
bsObj = BeautifulSoup(html.read(),"html.parser")发布于 2017-12-29 07:03:03
只需使用requests模块,这不是内置模块,而是第三方库.
import requests
from bs4 import BeautifulSoup
html = requests.get("http://pythonscraping.com/pages/page1.html").text
bsObj = BeautifulSoup(html, "html.parser")
print (bsObj.h1)顺便说一下,警告与request(s)库无关,但与BeautifulSoup解析器有关。
https://stackoverflow.com/questions/48018710
复制相似问题