- 帖子
- 13
- 精华
- 0
- 积分
- 56
- 阅读权限
- 20
- 注册时间
- 2020-1-14
- 最后登录
- 2023-7-14
|
为啥我这个爬虫输出的网络不给力,请稍后重试- import urllib.request
- import urllib.parse
- # 拼接URL
- basedurl = "http://baidu.com/s?"
- key = input("请输入要搜索的内容:")
- # 进行urlencode()编码
- wd = {"wd": key}
- key = urllib.parse.urlencode(wd)
- url = basedurl + key
- headers = {"User-Agent": "Mozilla/5.0"}
- # 创建请求对象
- req = urllib.request.Request(url, headers=headers)
- # 获取响应对象
- res = urllib.request.urlopen(req)
- html = res.read().decode("utf-8")
- # 写入本地文件
- with open("搜索.html", "w", encoding="utf-8") as f:
- f.write(html)
复制代码 打开 搜索.html 以后,这样的结果
|
|