Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python urlopen()的错误_Python_Python 3.x_Urlopen - Fatal编程技术网

Python urlopen()的错误

Python urlopen()的错误,python,python-3.x,urlopen,Python,Python 3.x,Urlopen,你好 用python 3*编写了以下代码,其中给出了一系列错误: from html.parser import HTMLParser import re from urllib.request import urlopen import glob your_hotel_adres="http://www.booking.com/hotel/ua/boutique-hotel-kavalier.ru.html" your_hotel_сс1=your_hotel_adres[29:31]

你好

用python 3*编写了以下代码,其中给出了一系列错误:

from html.parser import HTMLParser
import re
from urllib.request import urlopen
import glob

your_hotel_adres="http://www.booking.com/hotel/ua/boutique-hotel-kavalier.ru.html"

your_hotel_сс1=your_hotel_adres[29:31] 
your_hotel_pagename=''
i = 30
while i <= len(your_hotel_adres):
    i = i + 1
    if your_hotel_adres[i:i+5] == ".html":
        your_hotel_pagename=your_hotel_adres[32:i-3]
        break

your_hotel_comm="http://www.booking.com/reviewlist.ru.html?pagename="+your_hotel_pagename+";cc1="+your_hotel_сс1+";type=total;dist=1;offset=0;rows=1000"
f = urlopen(your_hotel_comm)
    >>> f=urlopen('http://www.booking.com/reviewlist.ru.html?pagename=boutique-hotel
-kavalier;cc1=ua;type=total;dist=1;offset=0;rows=1000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "C:\Python34\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python34\lib\urllib\request.py", line 493, in error
    result = self._call_chain(*args)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python34\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "C:\Python34\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python34\lib\urllib\request.py", line 499, in error
    return self._call_chain(*args)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 579, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
它应该下载一个网页并将其写入变量。所以我运行它,得到了很多很多错误:

from html.parser import HTMLParser
import re
from urllib.request import urlopen
import glob

your_hotel_adres="http://www.booking.com/hotel/ua/boutique-hotel-kavalier.ru.html"

your_hotel_сс1=your_hotel_adres[29:31] 
your_hotel_pagename=''
i = 30
while i <= len(your_hotel_adres):
    i = i + 1
    if your_hotel_adres[i:i+5] == ".html":
        your_hotel_pagename=your_hotel_adres[32:i-3]
        break

your_hotel_comm="http://www.booking.com/reviewlist.ru.html?pagename="+your_hotel_pagename+";cc1="+your_hotel_сс1+";type=total;dist=1;offset=0;rows=1000"
f = urlopen(your_hotel_comm)
    >>> f=urlopen('http://www.booking.com/reviewlist.ru.html?pagename=boutique-hotel
-kavalier;cc1=ua;type=total;dist=1;offset=0;rows=1000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "C:\Python34\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python34\lib\urllib\request.py", line 493, in error
    result = self._call_chain(*args)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python34\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "C:\Python34\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python34\lib\urllib\request.py", line 499, in error
    return self._call_chain(*args)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 579, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
请帮帮我! 刚刚注意到一件事:打开的页面的vbrauzere地址具有以下形式: 和地址,生成我的代码: -卡瓦利埃;cc1=ua;类型=总数;dist=1;偏移量=0;行数=1000
如何修复?

“HTTP错误400:错误请求”-服务器发送此消息。从理论上讲,你的代码没有问题。你明白HTTP错误400:错误请求的意思吗?如何编写正确的请求取决于站点。@J.F.Sebastian您能建议如何编写正确的请求吗?请在浏览器中访问站点,手动执行所需的操作,并查看浏览器发送的请求使用浏览器工具或外部网络嗅探器记录http请求,使用urllib.parse.urlencode根据嗅探到的信息创建正确的查询;在URL中看起来很奇怪。它们应该是符号&?