简体   繁体   English

是否可以使用python获取网页的前几个(例如1K)?

[英]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.

相关问题 如何使用Python每秒发送1k POST http请求? - How to send 1k POST http requests per second with data using Python? 在Python中使用尽可能少的行来获取第一个元素之前,是否可以检查列表是否为空? - Is it possible to check that a list is not empty before grabbing the first element using as few lines as possible in Python? MIFARE经典1k MFRC522-python库 - MIFARE classic 1k MFRC522-python lib 是否可以使用python request(?srn = true)库在网页中获取隐藏信息? - Is it possible to fetch the hidden info in webpage using python requests(?srn=true) library? 从 Python 中的 HTML 页面获取表的前几行 - Fetch first few rows of a table from a HTML page in Python 如何在python中获取完整的网页(使用javascript) - How to fetch complete webpage (using javascript) in python 使用python可以在网页中导入表格数据吗? - Table data import in webpage using python possible? 如何使用Python获取网页的页面源? - How can I fetch the page source of a webpage using Python? 如何将数字格式化为字符串(1k,2M ...)而不在 python 中四舍五入? - How to format numbers as strings (1k, 2M...) without rounding up in python? 在 python 中重写中文(应该是可写的)MIFARE 1K 卡上的 uid 和 block 0 - re-writing uid and block 0 on Chinese (supposed to be writable) MIFARE 1K card in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM