使用 Python 或 Curl 获取页面时的不同页面
Different page when fetching a page with Python or Curl
我正在将软件从 Python 重写为 Go,在请求以 iso-8859-1
编码的页面时遇到了一些问题。
此代码有效:
r = requests.get("https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=show_document&print=yes&highlight_docid=aza://27-01-2016-5A_718-2015")
r.encoding = 'iso-8859-1'
file = open('tmp_python.txt', 'w')
file.write(r.text.strip())
file.close()
最后一行是:
<script type="text/javascript">
var imgLoad = imagesLoaded( document.body );
imgLoad.on( 'always', function( instance ) {
window.print( );
} );
</script>
</html>
当我执行 时,最后一行是:
</body>
</html>
我认为问题出在 Go 中。然后我使用curl
获取资源,我也得到了错误的结果。因此,我认为问题出在其他地方。
我的浏览器获得了预期的文档。所以我添加了一个用户代理:
curl -A "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=show_document&print=yes&highlight_docid=aza://27-01-2016-5A_718-2015
我的浏览器像 Python 脚本一样获取预期的页面。 curl
和 Go 脚本都不是这种情况。对此有何解释?
服务器似乎查看请求的 User-Agent
字段 header 来决定要服务器的内容。当做一些简单的伪造时,你会得到与 Python 相同的内容,而浏览器会做:
client := &http.Client{}
req, err := http.NewRequest("GET", link, nil)
req.Header.Add("User-Agent","Mozilla/5.0")
resp, err := client.Do(req)
我在执行你建议的 curl 命令时也得到了这个内容,至少在正确引用 URL 之后是这样。
我正在将软件从 Python 重写为 Go,在请求以 iso-8859-1
编码的页面时遇到了一些问题。
此代码有效:
r = requests.get("https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=show_document&print=yes&highlight_docid=aza://27-01-2016-5A_718-2015")
r.encoding = 'iso-8859-1'
file = open('tmp_python.txt', 'w')
file.write(r.text.strip())
file.close()
最后一行是:
<script type="text/javascript">
var imgLoad = imagesLoaded( document.body );
imgLoad.on( 'always', function( instance ) {
window.print( );
} );
</script>
</html>
当我执行
</body>
</html>
我认为问题出在 Go 中。然后我使用curl
获取资源,我也得到了错误的结果。因此,我认为问题出在其他地方。
我的浏览器获得了预期的文档。所以我添加了一个用户代理:
curl -A "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" https://www.bger.ch/ext/eurospider/live/de/php/aza/http/index.php?lang=de&type=show_document&print=yes&highlight_docid=aza://27-01-2016-5A_718-2015
我的浏览器像 Python 脚本一样获取预期的页面。 curl
和 Go 脚本都不是这种情况。对此有何解释?
服务器似乎查看请求的 User-Agent
字段 header 来决定要服务器的内容。当做一些简单的伪造时,你会得到与 Python 相同的内容,而浏览器会做:
client := &http.Client{}
req, err := http.NewRequest("GET", link, nil)
req.Header.Add("User-Agent","Mozilla/5.0")
resp, err := client.Do(req)
我在执行你建议的 curl 命令时也得到了这个内容,至少在正确引用 URL 之后是这样。