iis服务器助手广告
返回顶部
首页 > 资讯 > 前端开发 > html >怎么用Web Scraping爬取HTML网页
  • 674
分享到

怎么用Web Scraping爬取HTML网页

2024-04-02 19:04:59 674人浏览 泡泡鱼
摘要

这篇文章主要讲解了“怎么用WEB Scraping爬取html网页”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Web Scraping爬取HTML网

这篇文章主要讲解了“怎么用WEB Scraping爬取html网页”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Web Scraping爬取HTML网页”吧!

  -爬取HTML网页

  -直接下载数据文件,例如csv,txt,pdf文件

  -通过应用程序编程接口(API)访问数据,例如 电影数据库,Twitter

  选择网页爬取,当然了解HTML网页的基本结构,可以参考这个网页:

  HTML的基本结构

  HTML标记:head,body,p,a,fORM,table等等

  标签会具有属性。例如,标记a具有属性(或属性)href的链接的目标。

  class和id是html用来通过级联样式表(CSS)控制每个元素的样式的特殊属性。 id是元素的唯一标识符,而class用于将元素分组以进行样式设置。

  一个元素可以与多个类相关联。 这些类别之间用空格隔开,例如 <h3 class=“ city main”>伦敦</ h3>

  下图是来自W3SCHOOL的例子,city的包括三个属性,main包括一个属性,London运用了两个city和main,这两个类,呈现出来的是下图的样子。

  可以通过标签相对于彼此的位置来引用标签

  child-child是另一个标签内的标签,例如 这两个p标签是div标签的子标签。

  parent-parent是一个标签,另一个标签在其中,例如 html标签是body标签的parent标签。

  siblings-siblings是与另一个标签具有相同parent标签的标签,例如 在html示例中,head和body标签是同级标签,因为它们都在html内。 两个p标签都是sibling,因为它们都在body里面。

  四步爬取网页:

  第一步:安装模块

  安装requests,beautifulsoup4,用来爬取网页信息

  Install modules requests, BeautifulSoup4/scrapy/selenium/....requests: allow you to send Http/1.1 requests using python. To install:Open terminal (Mac) or Anaconda Command Prompt (windows)code:  BeautifulSoup: web page parsing library, to install, use:

  第二步 :利用安装包来读取网页源码

  第三步:浏览网页源码找到需要读取信息的位置

  这里不同的浏览器读取源码有差异,下面介绍几个,有相关网页查询详细信息。

  Firefox: right click on the web page and select "view page source"Safari: please instruction here to see page source ()Ineternet Explorer: see instruction at

  第四步:开始读取

  Beautifulsoup: 简单那,支持CSS Selector, 但不支持 XPathscrapy (): 支持 CSS Selector 和XPathSelenium: 可以爬取动态网页 (例如下拉不断更新的)lxml等BeautifulSoup里Tag: an xml or HTML tag 标签Name: every tag has a name 每个标签的名字Attributes: a tag may have any number of attributes. 每个标签有一个到多个属性 A tag is shown as a dictionary in the form of {attribute1_name:attribute1_value, attribute2_name:attribute2_value, ...}. If an attribute has multiple values, the value is stored as a listNavigableString: the text within a tag

  上代码:

  #Import requests and beautifulsoup packages

  from IPython.core.interactiveshell import InteractiveShell

  InteractiveShell.ast_node_interactivity="all"

  # import requests package

  import requests

  # import BeautifulSoup from package bs4 (i.e. beautifulsoup4)

  from bs4 import BeautifulSoup

  Get web page content

  # send a get request to the web page

  page=requests.get("A simple example page")

  # status_code 200 indicates success.

  # a status code >200 indicates a failure

  if page.status_code==200:

  # content property gives the content returned in bytes

  print(page.content)  # text in bytes

  print(page.text)     # text in unicode

  #Parse web page content

  # Process the returned content using beautifulsoup module

  # initiate a beautifulsoup object using the html source and Python&rsquo;s html.parser

  soup=BeautifulSoup(page.content, 'html.parser')

  # soup object stands for the **root**

  # node of the html document tree

  print("Soup object:")

  # print soup object nicely

  print(soup.prettify())

  # soup.children returns an iterator of all children nodes

  print("\soup children nodes:")

  soup_children=soup.children

  print(soup_children)

  # convert to list

  soup_children=list(soup.children)

  print("\nlist of children of root:")

  print(len(soup_children))

  # html is the only child of the root node

  html=soup_children[0]

  html

  # Get head and body tag

  html_children=list(html.children)

  print("how many children under html? ", len(html_children))

  for idx, child in enumerate(html_children):

  print("Child {} is: {}\n".format(idx, child))

  # head is the second child of html

  head=html_children[1]

  # extract all text inside head

  print("\nhead text:")

  print(head.get_text())

  # body is the fourth child of html

  body=html_children[3]

  # Get details of a tag

  # get the first p tag in the div of body

  div=list(body.children)[1]

  p=list(div.children)[1]

  p

  # get the details of p tag

  # first, get the data type of p

  print("\ndata type:")

  print(type(p))

  # get tag name (property of p object)

  print ("\ntag name: ")

  print(p.name)

  # a tag object with attributes has a dictionary

  # use <tag>.attrs to get the dictionary

  # each attribute name of the tag is a key

  # get all attributes

  p.attrs

  # get "class" attribute

  print ("\ntag class: ")

  print(p["class"])

  # how to determine if 'id' is an attribute of p?

  # get text of p tag

  p.get_text()

感谢各位的阅读,以上就是“怎么用Web Scraping爬取HTML网页”的内容了,经过本文的学习后,相信大家对怎么用Web Scraping爬取HTML网页这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: 怎么用Web Scraping爬取HTML网页

本文链接: https://www.lsjlt.com/news/92598.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么用Web Scraping爬取HTML网页
    这篇文章主要讲解了“怎么用Web Scraping爬取HTML网页”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Web Scraping爬取HTML网...
    99+
    2024-04-02
  • Python用requests-html爬取网页的实现
    目录1. 开始2. 原理3. 元素定位css 选择器4. CSS 简单规则5. Xpath简单规则6. 人性化操作7. 加载 js8. 总结1. 开始 Python 中可以进行网页解...
    99+
    2024-04-02
  • python怎么爬取豆瓣网页
    这篇文章主要介绍了python怎么爬取豆瓣网页,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。python 语法简要介绍python 的基础语法大体与c语言相差不大,由于省去了...
    99+
    2023-06-14
  • 怎么使用matlab爬取网页图片
    要使用Matlab来爬取网页图片,可以使用以下步骤:1. 首先,需要安装和配置Matlab的Web Access Toolbox。这...
    99+
    2023-08-20
    matlab
  • 怎么使用python爬取网页图片
    本篇内容介绍了“怎么使用python爬取网页图片”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在现在这个信息爆炸的时代,要想高效的获取数据,...
    99+
    2023-07-02
  • nodejs怎么爬取动态网页
    要爬取动态网页,可以使用Node.js的一些库和工具,如Puppeteer、Cheerio和Axios。下面是一个使用这些工具的示例...
    99+
    2023-08-15
    nodejs
  • 怎么用python爬虫抓取网页文本
    使用Python爬虫抓取网页文本可以使用第三方库requests和beautifulsoup。首先,安装requests和beaut...
    99+
    2023-10-18
    python
  • 怎么利用Python批量爬取网页图片
    你可以使用Python的requests库来发起HTTP请求,并使用BeautifulSoup库来解析HTML文档以获取图片的URL...
    99+
    2023-09-27
    Python
  • python怎么爬取搜索后的网页
    要爬取搜索后的网页,可以使用Python中的爬虫库(如Requests、BeautifulSoup、Scrapy等)来发送HTTP请...
    99+
    2023-09-15
    python
  • 使用 Python 爬取网页数据
    1. 使用 urllib.request 获取网页 urllib 是 Python 內建的 HTTP 库, 使用 urllib 可以只需要很简单的步骤就能高效采集数据; 配合 Beautiful 等 HTML 解析库, 可以编写出用于采集...
    99+
    2023-01-31
    网页 数据 Python
  • Python 爬虫:如何用 BeautifulSoup 爬取网页数据
    在网络时代,数据是最宝贵的资源之一。而爬虫技术就是一种获取数据的重要手段。Python 作为一门高效、易学、易用的编程语言,自然成为了爬虫技术的首选语言之一。而 BeautifulSoup 则是 Py...
    99+
    2023-10-23
    python 爬虫 beautifulsoup
  • Python怎么爬取网页内容并存储
    本篇内容介绍了“Python怎么爬取网页内容并存储”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!前言想必大家都爬取过各个网站上首页上的东西把...
    99+
    2023-06-02
  • Pycharm怎么爬取网页文本和图片
    要使用Pycharm爬取网页文本和图片,你可以使用以下步骤:1. 导入所需的库:`requests`和`beautifulsoup4...
    99+
    2023-08-18
    Pycharm
  • 如何用C#+Selenium+ChromeDriver爬取网页
    小编今天带大家了解如何用C#+Selenium+ChromeDriver爬取网页,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“如何用...
    99+
    2023-06-29
  • 如何用python爬取网页数据
    要用Python爬取网页数据,可以使用Python的一些库和模块,例如requests、BeautifulSoup和Scrapy等。...
    99+
    2023-10-12
    python
  • 怎么用python爬取网站
    使用Python爬取网站的一般步骤如下:1. 导入所需的库,如`requests`或`urllib`用于发送HTTP请求,`beau...
    99+
    2023-08-31
    Python
  • python爬虫怎么抓取html
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-22
  • Python网络爬虫之Web网页基础是什么
    本文小编为大家详细介绍“Python网络爬虫之Web网页基础是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python网络爬虫之Web网页基础是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1.网页的...
    99+
    2023-07-05
  • 怎么用C#爬网页数据
    这篇文章主要讲解了“怎么用C#爬网页数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用C#爬网页数据”吧!CSQuery1. 安装github的地址:https://github.co...
    99+
    2023-06-16
  • python怎么爬取网页内的指定内容
    要爬取网页内的指定内容,可以使用Python中的第三方库,如BeautifulSoup和Requests。首先,需要安装这两个库。使...
    99+
    2023-08-08
    python
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作