首页 > 编程语言 >python实战:用requests+做爬虫

python实战:用requests+做爬虫

时间:2024-02-20 16:36:00浏览次数:32  
标签:shtml www cn python guancha 爬虫 2024 https requests

一,安装requests

1,用pip安装

(venv) liuhongdi@192 news % pip3 install requests

2,查看所安装库的版本:

(venv) liuhongdi@192 news % pip3 show requests
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /Users/liuhongdi/python_work/tutorial/news/venv/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by:

二,安装BeautifulSoup

1,用pip安装

(venv) liuhongdi@192 news % pip3 install beautifulsoup4

2,查看安装库的信息

(venv) liuhongdi@192 news % pip3 show beautifulsoup4
Name: beautifulsoup4
Version: 4.12.3
Summary: Screen-scraping library
Home-page:
Author:
Author-email: Leonard Richardson <[email protected]>
License: MIT License
Location: /Users/liuhongdi/python_work/tutorial/news/venv/lib/python3.12/site-packages
Requires: soupsieve
Required-by:

说明:刘宏缔的架构森林—专注it技术的博客,
网址:https://imgtouch.com
本文: https://blog.imgtouch.com/index.php/2024/02/20/python-shi-zhan-yong-requests-zuo-pa-chong/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

三,用requests+BeautifulSoup抓取页面并进行解析

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import requests from bs4 import BeautifulSoup   # 抓取观察者网的科技频道   # 网页中链接的主机地址 base_url = "https://www.guancha.cn"   # 要爬取的页面 url = "https://www.guancha.cn/GongYe%C2%B7KeJi/list_1.shtml" # 参数 params = {} # header headers = {     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" }   # 抓取 response = requests.get(url, params = params, headers = headers) # print(response.text)   # 解析 soup = BeautifulSoup(response.text, 'html.parser')   # 从中找到我们需要的元素 container = soup.find('ul', {'class': 'column-list fix'})   # 从ul下得到所有的li nodes = container.find_all('li')   # 遍历 for node in nodes:     link = base_url + node.find('a')['href']     print(link)     text = node.find('a').string     print(text)

运行结果:

https://www.guancha.cn/industry-science/2024_02_20_725732.shtml
在南极取得突破!国产极地重型载具完成技术测试
https://www.guancha.cn/industry-science/2024_02_20_725718.shtml
我国科学家发现非常规反铁磁体
https://www.guancha.cn/industry-science/2024_02_17_725482.shtml
我国猴痘mRNA疫苗将进入临床试验
https://www.guancha.cn/industry-science/2024_02_17_725468.shtml
首飞失败后,日本新型H3火箭2号机发射升空
https://www.guancha.cn/industry-science/2024_02_17_725467.shtml
“我们真的看到新工业革命来临”?
https://www.guancha.cn/industry-science/2024_02_17_725454.shtml
OpenAI视频生成模型,会让哪些人失业?
https://www.guancha.cn/industry-science/2024_02_16_725430.shtml
OpenAI发布首个视频生成模型:输文字出视频,1分钟流畅高清
https://www.guancha.cn/industry-science/2024_02_16_725411.shtml
自主研制离子成像技术探测量子态,我国科学家有了新发现
https://www.guancha.cn/industry-science/2024_02_14_725323.shtml
新春伊始,一批大国重器取得新突破
https://www.guancha.cn/industry-science/2024_02_14_725314.shtml
微型机器人在国际空间站首次模拟手术任务
https://www.guancha.cn/industry-science/2024_02_12_725209.shtml
实现突破性进展!这一领域,我国处于全球第一梯队
https://www.guancha.cn/industry-science/2024_02_08_724888.shtml
向理解高温超导机理迈出重要一步,中国科学家首次观测到
https://www.guancha.cn/industry-science/2024_02_07_724761.shtml
Vision Pro开卖炸出各种显眼包,有人戴着开车…
https://www.guancha.cn/industry-science/2024_02_07_724738.shtml
我国编制首部脑机接口研究伦理指引
https://www.guancha.cn/industry-science/2024_02_05_724538.shtml
英伟达对华“阉割版”芯片已可接受预订,但经销商说…
https://www.guancha.cn/industry-science/2024_02_02_724305.shtml
研究:月球正在缩小,南极月震使月球基地可能没那么宜居
https://www.guancha.cn/industry-science/2024_02_02_724227.shtml
这项重大突破,避免了“美国人比中国人更了解中国人”
https://www.guancha.cn/industry-science/2024_02_01_724214.shtml
此前只有两个国家掌握这一技术,我国实现突破
https://www.guancha.cn/industry-science/2024_01_31_724012.shtml
对标GPT-4,讯飞星火V3.5发布
https://www.guancha.cn/industry-science/2024_01_30_723955.shtml
AI作品是否享有著作权?北京互联网法院曾判决支持

标签:shtml,www,cn,python,guancha,爬虫,2024,https,requests
From: https://www.cnblogs.com/architectforest/p/18023407

相关文章

  • Python 实现Excel和CSV格式之间的互转
    通过使用Python编程语言,编写脚本来自动化Excel和CSV之间的转换过程,可以批量处理大量文件,定期更新数据,并集成转换过程到自动化工作流程中。本文将介绍如何使用第三方库Spire.XLSforPython实现:使用Python将Excel转为CSV使用Python将CSV转为Excel安装PythonExcel类库:pip......
  • python文件获取并读取固定长度数据实例解析
    一概念1file操作:文件操作一般有open,write,read,close几种,这里重点是read固定长度数据。read() 用于从文件读取指定的字节数,如果未给定或为负则读取所有。本文中心不在概念,直接上源码。二源码解析importsysfromPyQt5importQtWidgetsfromPyQt5.QtWidgetsimportQF......
  • python不能跳转进入某个函数或模块的一种解决思路
    例如,下图中的get_bucket_mount_root函数可以顺利import进来,但是按ctrl键不能跳转进入这个函数: 一个解决思路是,在vscode终端中,打开python解释器,import上图中的hatbc库,然后用hatbc.__file__命令查找该库的__init__.py文件的路径,按住ctrl键,点击这个路径,即可跳转进入这个__init__.......
  • 爬虫_02days
    免费代理池搭建#代理有免费和收费代理#代理有http代理和https代理#匿名度 -高匿:隐藏访问者ip-透明:服务端能拿到访问者ip-作为后端,如何拿到使用代理人的ip -请求头中:x-forword-for-如果一个HTTP请求到达服务器之前,经过了三个代理Proxy1、Proxy2、Proxy3......
  • python · matplotlib | seaborn 画图与调整图例位置
    1seaborn画图代码存档:sns.set_style("whitegrid")#好看的styleplt.figure()#plt.plot(ppo_data['Step']*step_mul,ppo_data['ppo_mean'],label='PPO')#plt.plot(sac_data['Step']*step_mul,sac_data['sac_m......
  • [python] [mongoDB] pymongo -- 用python操作mongodb
    官方文档数据库格式mongodb采用了BSON格式,即database->collection->document,在python中,pymongo使用字典来表示一个documnet;document可以包含python原生的数据类型,比如datetime.datetime连接数据库MongoClient连接mongodb,读取数据库,集合和文档CRUD插入Collect......
  • Python计算两图相似性-哈希算法(Hash)
    1、简介aHash:平均值哈希。速度比较快,但是常常不太精确。pHash:感知哈希。精确度比较高,但是速度方面较差一些。dHash:差异值哈希。精确度较高,均值哈希算法、差值哈希算法和感知哈希算法都是值越小,相似度越高,取值为0-64,即汉明距离中,64位的hash值有多少不同。三直方图和单通道直方图......
  • PC应用程序自动化(python)
    个人向笔记。看的是BV14Y4y1z7z6这个视频。假期感兴趣看了一点点,感觉和我的日常工作匹配度不太高,所以先记录到这里,后面再补充。 PC自动化--pywinauto0、前置0.1是个后端的访问。application:作用范围是一个进程; desktop:作用范围可以跨进程。 0.2程序检测辅助工具 ......
  • python调用qq邮箱发送邮件
    代码如下,需要qq邮箱开启授权码importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeadermessage=MIMEText('邮件内容')#邮件内容message['From']=Header('[email protected]')#邮件发送者名字message['To']=Header(&#......
  • Ubuntu 安装 Python3.6.7
    注意:不要卸载ubuntu自带的python版本;ubuntu下不同版本的python可以共存,可直接安装python3.6。1.升级包索引和软件sudoaptupdatesudoaptupgrade-y2.安装编译所需包sudoaptinstallbuild-essentialzlib1g-devlibbz2-devlibncurses5-devlibgdbm-devlibns......