爬虫第4课:get请求

注意下面这些代码

  • 这个脚本直接打印出网页的HTML内容,而不是解析后的内容。如果你想要解析网页内容(如提取某些特定信息),你可能需要使用如BeautifulSoup之类的库。
  • 这个脚本没有进行错误处理,例如网络错误或请求超时等。在实际应用中,你可能需要添加适当的错误处理机制。
  • 使用 fake_useragent 生成随机用户代理有助于避免某些网站的防爬虫机制,但这并不能保证一定能成功绕过所有的防爬虫机制。某些网站可能会采用更复杂的策略来检测和阻止爬虫。
复制代码
# Pythonit教程网(blog.pythonit.cn)
# Python全栈视频课件获取:www.dqu.cc
# 加速高防cdn:woaiyundun.cn
复制代码
# coding:utf-8
# 时间:2024/3/16 16:16
# Pythonit教程网(blog.pythonit.cn)
# Python全栈视频课件获取:www.dqu.cc
# 加速高防cdn:woaiyundun.cn
from urllib.request import urlopen,Request
from fake_useragent import UserAgent
from urllib.parse import quote
search = input("请输入搜索的内容:")
url = f"https://blog.pythonit.cn/index.php/search/{quote(search)}"
ua = UserAgent()
headers = {
    'User-Agent': ua.chrome
}
re = Request(url,headers=headers)
reopen = urlopen(re)
print(reopen.read().decode())
相关推荐
孟健9 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞11 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽13 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敲敲敲敲暴你脑袋17 小时前
写个添加注释的vscode插件
javascript·typescript·visual studio code
golang学习记18 小时前
GitLens 十大神技:彻底改变你在 VS Code 中的 Git 工作流
前端·后端·visual studio code
敏编程18 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪18 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook18 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain