首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >urllib.requests或urllib.request

urllib.requests或urllib.request
EN

Stack Overflow用户
提问于 2017-12-29 06:54:31
回答 2查看 189关注 0票数 0

在"urlib“库下,无论我们有‘request’模块还是'request'模块

运行以下代码时

代码语言:javascript
复制
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正在抛出警告

代码语言:javascript
复制
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>

当我这么做的时候

代码语言:javascript
复制
>>>import requests

哪个shell成功导入请求模块

但是当我将上面的代码更改为

代码语言:javascript
复制
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抛出错误消息。

代码语言:javascript
复制
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工具成功地安装了两个模块(请求,请求)

代码语言:javascript
复制
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...
EN

回答 2

Stack Overflow用户

发布于 2017-12-29 07:02:30

urllib是它拥有的一个模块,对于requests来说也是一样的。你做了:

代码语言:javascript
复制
import requests
from urllib.request import urlopen

它是urllib.request而不是urllib.requests,它解释了python解释器给您带来的错误。这是文档 for urllib

至于您正在获得的第一个shell错误,python已经提示您如何解决它。因此,与其:

代码语言:javascript
复制
bsObj = BeautifulSoup(html.read())

你应该这么做:

代码语言:javascript
复制
bsObj = BeautifulSoup(html.read(),"html.parser")
票数 0
EN

Stack Overflow用户

发布于 2017-12-29 07:03:03

只需使用requests模块,这不是内置模块,而是第三方库.

代码语言:javascript
复制
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解析器有关。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48018710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档