[英]Is it possible to fetch the first few, say 1K, of a webpage using python?
Is it possible to fetch the first few, say 1K, of a webpage using python? 是否可以使用python获取网页的前几个(例如1K)?
Thank you very much! 非常感谢你!
The Requests library lets you iterate over the response as it comes in so you could do something like this: Requests库可让您遍历传入的响应,因此您可以执行以下操作:
import requests
beginning = requests.get('http://example.com/').iter_content(1024).next()
If you just want the headers you can always use the the http HEAD method: 如果只需要标题,则可以始终使用http HEAD方法:
req = requests.head('http://example.com')
Here's an example using Python 3's urllib.request , which is built in. 这是使用Python 3内置的urllib.request的示例。
import urllib.request
url = urllib.request.openurl("http://example.com").read(1024)
Sure: 当然:
>>> len(urllib2.urlopen('http://google.com').read(1024))
1024
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.