python - python urllib,返回特定url的空页面
问题描述
我在使用 urllib 的特定链接时遇到问题。下面是我使用的代码示例:
from urllib.request import Request, urlopen
import re
url = ""
req = Request(url)
html_page = urlopen(req).read()
print(len(html_page))
这是我从两个链接得到的结果:
url = "https://www.dafont.com"
Length: 0
url = "https://www.stackoverflow.com"
Length: 196673
有人知道为什么会这样吗?
解决方案
尝试使用。你会得到回应。某些网站是安全的,仅响应某些用户代理。
from urllib.request import Request, urlopen
url = "https://www.dafont.com"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
req = Request(url, headers=headers)
html_page = urlopen(req).read()
print(len(html_page))
推荐阅读
- c# - HelpProvider 在某些控件上忽略表单的 HelpTopic
- elasticsearch - ElasticSearch - 如何在“多级”聚合查询中显示附加字段名称?
- c++ - 当元素数量可变时,在向量中从用户那里获取输入
- azure-active-directory - Keycloak 可以为 AD 用户保留身份提供程序会话吗
- javascript - 在不同的屏幕尺寸上显示和隐藏内容
- sql - 替换 SQL 中性能不佳的游标
- c# - 无法更新 HubSpot 中的时间戳属性
- python - 这个错误是什么意思,我不明白 '[1006/094704.464:INFO:CONSOLE(1)] "ERROR [object Object]"
- clojure - 列表中的唯一元素
- api - 进度指示器的状态没有变化