python-3.x - Python 3 - 将自定义 header 添加到 urllib.request 请求

标签 python-3.x web-crawler python-requests urllib

Python 3中,以下代码获取网页的 HTML 源代码。

import urllib.request
url = "https://docs.python.org/3.4/howto/urllib2.html"
response = urllib.request.urlopen(url)

response.read()

使用 urllib.request 时如何将以下自定义 header 添加到请求中?

headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

最佳答案

可以通过首先创建请求对象然后将其提供给 urlopen 来自定义请求 header 。

import urllib.request
url = "https://docs.python.org/3.4/howto/urllib2.html"
hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

req = urllib.request.Request(url, headers=hdr)
response = urllib.request.urlopen(req)
response.read()

来源:Python 3.4 Documentation

关于python-3.x - Python 3 - 将自定义 header 添加到 urllib.request 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47029280/

相关文章:

python-3.x - pytest argparse 测试用例不起作用

python - 如何获取光标前的字符? Tkinter

Python3找到最后一次出现的字符串然后写入

python - Unicode解码错误: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)

Python 请求 - 无法解析 URL

python - 使用Python获取https网站时超时

java - 使用相对路径打开 url

java - 如何在Java中使用Sitemap进行网络爬虫?

c# - 遍历 DirectoryEntry 或任何对象层次结构 - C#

python - 试图从网站上抓取视频帧。获取 403 : Forbidden and 'wrong cookie'