【问题标题】:how to load a page through a script?如何通过脚本加载页面?
【发布时间】:2014-02-23 10:55:19
【问题描述】:

请帮助修复脚本。

import pprint
import requests

import bs4

topicsWatch = [
                'http://forum.saransk.ru/',
                'http://forum.saransk.ru/topic/194169-v-kakuiu-sportivnuiu-sektciiu-mozhno-zapisat-reben/',
]

#1
html = requests.get(topicsWatch[0])
print(topicsWatch[0], end='\n')
print(html.status_code )    #OK
print(html.raise_for_status())

#2
html = requests.get(topicsWatch[1])
print(topicsWatch[1], end='\n')
print(html.status_code )    #?????????????
print(html.raise_for_status())

有两个代码块。 “#1”和“2”。 块“#1”将返回响应“200” 块“#2”将返回响应“404”。

我不明白这一点。两个页面都实际存在:

http://forum.saransk.ru/
http://forum.saransk.ru/topic/194169-v-kakuiu-sportivnuiu-sektciiu-mozhno-zapisat-reben/

如何在这两种情况下都这样做,返回一个“200”?

输出:

>>> ================================ RESTART ================================
>>> 
http://forum.saransk.ru/
200
None
http://forum.saransk.ru/topic/194169-v-kakuiu-sportivnuiu-sektciiu-mozhno-zapisat-reben/
404
Traceback (most recent call last):
  File "C:\VINT\OPENSERVER\OpenServer\domains\localhost\python\parse_html\5_forum_new\q.py", line 24, in <module>
    print(html.raise_for_status())
  File "C:\Python33\lib\site-packages\requests\models.py", line 773, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found
>>> 

【问题讨论】:

    标签: python python-3.x beautifulsoup python-requests


    【解决方案1】:

    不,第二页 (http://forum.saransk.ru/topic/194169-v-kakuiu-sportivnuiu-sektciiu-mozhno-zapisat-reben/) 不存在。

    你会得到一个很好的错误页面,说它不存在,但这是一个默认页面。您也可以为 http://forum.saransk.ru/topic/invalid-page 获得相同的页面 - 例如。

    对于该页面,他们使用 nginx-server,您可以在其中定义如果使用 error_page - property 请求不存在的页面时要显示哪些内容,其他 Web 服务器具有类似的设置,这样您就不会最终得到一个旧的丑陋的 404 页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2011-01-13
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多