python web应用

Living in today’s world, we are surrounded by different data all around us. The ability to collect and use this data in our projects is a must-have skill for every data scientist.

生活在当今世界中,我们周围遍布着不同的数据。 在我们的项目中收集和使用这些数据的能力是每位数据科学家必不可少的技能。

There are so many tutorials online about how to use specific Python libraries to harvest online data. However, you can rarely find tutorials on choosing the best library for your particular application.

在线上有太多关于如何使用特定的Python库来收集在线数据的教程。 但是,您很少会找到有关为特定应用程序选择最佳库的教程。

Python offers a variety of libraries that one can use to scrape the web, libraires such as Scrapy, Beautiful Soup, Requests, Urllib, and Selenium. I am quite sure that more libraries exist, and more will be released soon considering how popular Python is.

Python提供了多种可用于抓取网络的库,例如Scrapy,Beautiful Soup,Requests,Urllib和Selenium等库。 我很确定存在更多的库,并且考虑到Python的流行程度,很快就会发布更多的库。

In this article, I will cover the 5 libraries I just mentioned, will give an overview of each of them, for example, code and what are the best applications and cases for each of them.

在本文中,我将介绍我刚刚提到的5个库,并对它们进行概述,例如,代码以及对它们而言最佳的应用程序和案例。

For the rest of this article, I will use this sandbox website containing books to explain specific aspects of each library.

对于本文的其余部分,我将使用这个沙箱网站包含的书来解释每个库的具体方面。

1.崎cra (1. Scrapy)

Scrapy is one of the most popular Python web scrapping libraries right now. It is an open-source framework. This means it is not even a library; it is rather a complete tool that you can use to scrape and crawl around the web systematically.

Scrapy是目前最流行的Python Web抓取库之一。 这是一个开源框架。 这意味着它甚至不是图书馆。 它是一个完整的工具,可用于系统地在网络上抓取和爬网。

Scrapy was initially designed to build web spiders that can crawl the web on their own. It can be used in monitoring and mining data, as well as automated and systematic testing.

Scrapy最初旨在构建可自动爬网的网络蜘蛛。 它可以用于监视和挖掘数据以及自动和系统的测试。

It is also very CPU and memory effecient compared to other Python approaches to scrape the web. The downside to using Scrapy is that installing it and getting to work correctly on your device can be a bit of a hassle.

与其他刮取Web的Python方法相比,它在CPU和内存方面也非常有效。 使用Scrapy的缺点是安装它并在您的设备上正常工作可能有点麻烦。

概述和安装 (Overview and installation)

To get started with Scrapy, you need to make sure that you’re running Python 3 or higher. To install Scrapy, you can simply write the following command in the terminal.

要开始使用Scrapy,您需要确保运行的是Python 3或更高版本。 要安装Scrapy,您只需在终端中编写以下命令即可。

pip install scrapy

Once Scrapy is successfully installed, you can run the Scrapy shell, by typing:

成功安装Scrapy之后,您可以通过键入以下命令运行Scrapy Shell:

scrapy shell

When you run this command, you will see something like this:

运行此命令时,您将看到以下内容:

Author’s screenshot
作者的屏幕截图

You can use the Scrapy shell to run simple commands, for example, you can fetch the HTML content of a website using the fetch function. So, let's say I want to fetch this book website; I can simply do that it in the shell.

您可以使用Scrapy shell来运行一些简单的命令,例如,您可以获取使用网站HTML内容fetch功能。 所以,让我们说,我想取这本书的网站; 我可以在外壳中简单地做到这一点。

fetch("http://books.toscrape.com/")

Now, you can then use the view function to open up this HTML file in your default browser. Or you can just print out the HTML source code of the page.

现在,您可以使用view功能在默认浏览器中打开此HTML文件。 或者,您可以只打印页面HTML源代码。

view(response)print(response.text)

Of course, you won’t be scaring a website just to open it in your browser. You probably want some specific information from the HTML text. This is done using CSS Selectors.

当然,您不会只是在浏览器中打开网站而感到害怕。 您可能需要HTML文本中的一些特定信息。 这是使用CSS选择器完成的

You will need to inspect the structure of the webpage you want to fetch before you start so you can use the correct CSS selector.

在开始之前,您需要检查要获取的网页的结构,以便可以使用正确CSS选择器。

什么时候使用Scarpy? (When to use Scarpy?)

The best case to use Scrapy is when you want to do a big-scale web scraping or automate multiple tests. Scrapy is very well-structured, which allows for better flexibility and adaptability to specific applications. Moreover, the way Scrapy projects are organized makes it easier o maintain and extend.

最好使用Scrapy的情况是要进行大规模的Web抓取或自动化多个测试时。 Scrapy的结构非常好,可以为特定应用程序提供更好的灵活性和适应性。 此外,Scrapy项目的组织方式使维护和扩展变得更容易。

I would suggest that you avoid using Scrapy if you have a small project or you want to scrape one or just a few webpages. In this case, Scarpy will overcomplicate things and won’t add and benefits.

如果您的项目很小,或者只想抓取一个或几个网页,我建议您避免使用Scrapy。 在这种情况下,Scarpy将使事情变得复杂,并且不会增加收益。

2.要求 (2. Requests)

Requests is the most straightforward HTTP library you can use. Requests allow the user to sent requests to the HTTP server and GET response back in the form of HTML or JSON response. It also allows the user to send POST requests to the server to modify or add some content.

请求是您可以使用的最直接的HTTP库。 请求允许用户将请求发送到HTTP服务器,并以HTML或JSON响应的形式返回GET响应。 它还允许用户向服务器发送POST请求,以修改或添加一些内容。

Requests show the real power that can be obtained with a well designed high-level abstract API.

请求显示了使用精心设计的高级抽象API可以获得的真正功能。

概述和安装 (Overview and installation)

Requests is often included in Python’s built-in libraries. However, if for some reason you can’t import it right away, you can install it easily using pip.

请求通常包含在Python的内置库中。 但是,如果由于某种原因无法立即导入,则可以使用pip轻松安装。

pip install requests

You can use Requests to fetch and clean well-organized API responses. For example, let’s say I want to look up a movie in the OMDB database. Requests allow me to send a movie name to the API, clean up the response, and print it in less than 10 lines of code — if we omit the comments

python web应用_为您的应用选择最佳的Python Web爬网库相关推荐

  1. 大学开设python课程吗_在大学为什么你一定要学会Python?

    之前,网上流传一组<人工智能实验教材>的图片,照片火起来的原因是教材是为幼儿园的小朋友们设计的! Python被列入小学.初高中教材已不是新鲜事,现在又成功"入侵"了幼 ...

  2. 知道经纬度用python画路线图_神级程序员教你用Python如何画一个中国地图!(好好玩)...

    为什么是Python 先来聊聊为什么做数据分析一定要用 Python 或 R 语言.编程语言这么多种, Java , PHP 都很成熟,但是为什么在最近热火的数据分析领域,很多人选择用 Python ...

  3. python log函数_求你别再花大价钱学 Python 之爬虫实战

    引子 Python 基本概念 Python 优势和劣势 优势 Python 的劣势 Python 安装设置 Python 基本语法 程序例子 Python 基本语法 Python 爬虫实现 爬虫相关 ...

  4. python 可视化 皮肤_为什么我不建议你轻易入Python的“坑”?

    2020年,如果你问我最值得学什么,那么我会毫不犹豫地告诉你--Python. 不久前,野村证券副首席数字官马修·汉普森在伦敦Quant Conference上发表讲话:"现在走进交易大厅, ...

  5. python网络安全设计_专为渗透测试人员设计的 Python 工具大合集

    如果你对漏洞挖掘.逆向工程分析或渗透测试感兴趣的话,我第一个要推荐给你的就是Python编程语言.Python不仅语法简单上手容易,而且它还有大量功能强大的库和程序可供我们使用.在这篇文章中,我们会给 ...

  6. 420集的python教程下载_微软官方发布了最新420集Python教程,这教程简直就是编程界福利...

    泰国清迈-2014 年 10 月 22 日: 微软主页 clos Austria, Tyrol, Axamer Lizum, hosting village of 近日,微软上线了一套 Python ...

  7. python软件工程师_为什么每个软件工程师都应该学习Python?

    python软件工程师 大家好,如果您经常关注我的博客 ,或者在HackerNoon上阅读我的文章,那么您可能想知道为什么我要写一篇文章告诉人们学习Python ? 几年前,我不是要您更喜欢Java吗 ...

  8. python 功能 代码_挑战“不可能”的代码:你不知道的Python功能

    本文转载自公众号"读芯术"(ID:AI_Discovery) Python看似简单,但实则变化万千,笔者总都能看到一些代码以近乎不可能的方式呈现.这些功能很精妙,以至于我无法想象没 ...

  9. python partition函数_如何使用正确的姿势进行高效Python函数式编程?

    演讲者:丁来强@Splunk  PyConChina2015 北京站 9月12日与9月19日,PyConChina 2015上海站与北京站顺利落下帷幕."人生苦短,Python 当歌&quo ...

最新文章

  1. backup ram不稳定 stm32_STM32学习笔记
  2. ERROR: JVMPI, an experimental interface, is no longer supported.
  3. C#编程语言之byte[]数组和string的互相转化
  4. 数字信号处理学习笔记(三)|时域离散系统的网络结构
  5. IM云将给在线教育带来哪些变革?
  6. 几十万实例线上系统的抖动问题定位
  7. SendMessage 循环按1 到6 F1到F8_2
  8. 使用Azure云原生构建博客是怎样一种体验?(下篇)
  9. json解析对应的value为null_徒手撸一个JSON解析器
  10. 最新 IDEA 2022.1 版本即将发布,骚操作真不少...
  11. Mujoco制作模拟视频
  12. STM32F030使用RTC周期性唤醒STOP模式
  13. Python+Matplotlib科研绘图
  14. 企业风险管理与内部控制常见问题与思考
  15. 25个超酷手绘插图的网站设计
  16. 数据挖掘十大经典算法(转存)
  17. 批发进销存软件哪个好用?求测评
  18. excel设置行高_手把手教你用wps表格excel制作田字格书法练习字帖
  19. 1. Java是编译型语言还是解释型语言?
  20. Vmware虚拟机的介绍

热门文章

  1. 判断字符串出栈合法性
  2. 全套学习!mysql命令窗口执行sql文件
  3. Intent 简单用法
  4. 设计模式学习(三)——单例模式
  5. iphone手机型号获取
  6. BZOJ2597 WC2007剪刀石头布(费用流)
  7. Python学习---django知识补充之CBV
  8. 51nod1832(二叉树/高精度模板+dfs)
  9. linux-Centos7安装nginx
  10. scp跨主机拷贝工具