python 网页编程

The internet and the World Wide Web (WWW), is probably the most prominent source of information today. Most of that information is retrievable through HTTP. HTTP was invented originally to share pages of hypertext (hence the name Hypertext Transfer Protocol), which eventually started the WWW.

互联网和万维网(WWW)可能是当今最突出的信息来源。 大多数信息可通过HTTP检索。 最初是发明HTTP来共享超文本页面的(因此被称为超文本传输​​协议),该页面最终启动了WWW。

This process occurs every time we request a web page through our devices. The exciting part is we can perform these operations programmatically to automate the retrieval and processing of information.

每当我们通过设备请求网页时,都会发生此过程。 令人兴奋的部分是我们可以以编程方式执行这些操作,以自动进行信息的检索和处理。

This article is an excerpt from the book Python Automation Cookbook, Second Edition by Jamie Buelta, a comprehensive and updated edition that enables you to develop a sharp understanding of the fundamentals required to automate business processes through real-world tasks, such as developing your first web scraping application, analyzing information to generate spreadsheet reports with graphs, and communicating with automatically generated emails.

本文摘自 Jamie Buelta撰写 的《 Python Automation Cookbook,第二版》 ,这是一个全面而更新的版本,使您能够深入了解通过实际任务(例如,开发第一个任务)来实现业务流程自动化的基本原理。网络抓取应用程序,分析信息以生成带有图表的电子表格报告,以及与自动生成的电子邮件进行通信。

In this article, we will learn how to leverage the Python language to fetch HTTP. Python has an HTTP client in its standard library. Further, the fantastic request modules make obtaining web pages very convenient.

在本文中,我们将学习如何利用Python语言来获取HTTP。 Python在其标准库中有一个HTTP客户端。 此外,出色的请求模块使获取网页非常方便。

[Related article: Web Scraping News Articles in Python]

[相关文章: Python中的Web搜刮新闻文章 ]

与表格互动 (Interacting with forms)

A common element present in web pages is forms. Forms are a way of sending values to a web page, for example, to create a new comment on a blog post, or to submit a purchase.

网页中常见的元素是表单。 表单是一种将值发送到网页的方法,例如,在博客文章上创建新评论或提交购买。

Browsers present forms so you can input values and send them in a single action after pressing the submit or equivalent button. We’ll see how to create this action programmatically in this recipe.

浏览器显示表单,因此您可以输入值并在按下提交或等效按钮后以单个操作发送它们。 我们将在本食谱中了解如何以编程方式创建此动作。

https://odsc.com/https://odsc.com/

做好准备 (Getting ready)

We’ll work against the test server https://httpbin.org/forms/post, which allows us to send a test form and sends back the submitted information.

我们将针对测试服务器https://httpbin.org/forms/post进行工作,该服务器允许我们发送测试表单并发回已提交的信息。

The following is an example form to order a pizza:

以下是订购比萨饼的示例表格:

Figure 1 Rendered form

图1呈现的表单

You can fill the form in manually and see it return the information in JSON format, including extra information such as the browser being used.

您可以手动填写表单,然后查看它以JSON格式返回信息,包括其他信息,例如正在使用的浏览器。

The following is the frontend of the web form that is generated:

以下是生成的Web表单的前端:

Figure 2: Filled-in form

图2:填写表格

The following screenshot shows the backend of the web form that is generated:

以下屏幕快照显示了生成的Web表单的后端:

Figure 3: Returned JSON content

图3:返回的JSON内容

We need to analyze the HTML to see the accepted data for the form. The source code is as follows:

我们需要分析HTML以查看表单的可接受数据。 源代码如下:

Figure 4: Source code

图4:源代码

Check the names of the inputs, custname, custtel, custemail, size (a radio option), topping (a multiselection checkbox), delivery (time), and comments.

检查输入的名称,客户名称,客户名称,客户邮件,大小(单选),打顶(多选复选框),传递(时间)和注释。

怎么做… (How to do it…)

1. Import the requests, BeautifulSoup, and re modules:

1.导入请求,BeautifulSoup,然后重新模块:

>>> import requests >>> from bs4 import BeautifulSoup >>> import re

2. Retrieve the form page, parse it, and print the input fields. Check that the posting URL is /post (not /forms/post): >>> response = requests.get(‘https://httpbin.org/forms/post’)

2.检索表单页面,对其进行解析,然后打印输入字段。 检查发布URL是否为/ post(不是/ forms / post): >>> response = requests.get('https://httpbin.org/forms/post')

>>> page = BeautifulSoup(response.text) >>> form = page.find('form') >>> {field.get('name') for field in form.find_all(re. compile('input|textarea'))} {'delivery', 'topping', 'size', 'custemail', 'comments', 'custtel', 'custname'}

3. Note that textarea is a valid input and is defined in the HTML format. Prepare the data to be posted as a dictionary. Check that the values are as defined in the form:

3.请注意,textarea是有效输入,并以HTML格式定义。 准备要作为字典发布的数据。 检查值是否符合以下格式中的定义:

>>> data = {'custname': "Sean O'Connell", 'custtel': '123-456- 789', 'custemail': 'sean@oconnell.ie', 'size': 'small', 'topping': ['bacon', 'onion'], 'delivery': '20:30', 'comments': ''}

4. Post the values and check that the response is the same as returned in the browser:

4.发布值,并检查响应是否与浏览器中返回的相同:

>>> response = requests.post('https://httpbin.org/post', data) >>> response <Response [200]> >>> response.json() {'args': {}, 'data': '', 'files': {}, 'form': {'comments': '', 'custemail': 'sean@oconnell.ie', 'custname': "Sean O'Connell", 'custtel': '123-456-789', 'delivery': '20:30', 'size': 'small', 'topping': ['bacon', 'onion']}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Content-Length': '140', 'Content-Type': 'application/x-wwwform- urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'pythonrequests/ 2.22.0'}, 'json': None, 'origin': '89.100.17.159', 'url': 'https://httpbin.org/post'}

这个怎么运作… (How it works…)

Requests directly encodes and sends data in the configured format. By default, it sends POST data in the application/x-www-form-urlencoded format.

请求以配置的格式直接编码并发送数据。 默认情况下,它以application / x-www-form-urlencoded格式发送POST数据。

The key aspect here is to respect the format of the form and the possible values that can return an error if incorrect, typically a 400 error, indicating a problem with the client.

此处的关键方面是尊重表单的格式和可能的值,如果不正确,则可能返回错误,通常为400错误,这表明客户端存在问题。

[Related article: Building a Scraper Using Browser Automation]

[相关文章: 使用浏览器自动化构建刮板 ]

还有更多… (There’s more…)

Other than following the format of forms and inputting valid values, the main problem when working with forms is the multiple ways of preventing spam and abusive behavior. You will often have to ensure that you have downloaded a form before submitting it, to avoid submitting multiple forms or Cross-Site Request Forgery (CSRF).

除了遵循表格的格式和输入有效值外,使用表格时的主要问题还在于防止垃圾邮件和滥用行为的多种方法。 您通常必须确保在提交表单之前已经下载了表单,以避免提交多个表单或跨站点请求伪造 ( CSRF )。

To obtain the specific token, you need to first download the form, as shown in the recipe, obtain the value of the CSRF token, and resubmit it. Note that the token can have different names; this is just an example:

要获取特定令牌,您需要先下载表单,如配方所示,获取CSRF令牌的值,然后重新提交。 请注意,令牌可以具有不同的名称。 这只是一个例子:

>>> form.find(attrs={'name': 'token'}).get('value') 'ABCEDF12345'

In this article, we learned how to obtain data from the forms of the web, parse it, and print the input fields using Python’s HTTP client. We also explored the role and application of requests, Beautiful Soup, and re–modules.

在本文中,我们学习了如何使用Python的HTTP客户端从Web表单中获取数据,进行解析并打印输入字段。 我们还探讨了请求,“美丽的汤”和“重新模块”的作用和应用。

关于作者 (About the Author)

Jaime Buelta is a full-time Python developer since 2010 and a regular speaker at PyCon Ireland. He has been a professional programmer for over two decades with a rich exposure to a lot of different technologies throughout his career. He has developed software for a variety of fields and industries, including aerospace, networking and communications, industrial SCADA systems, video game online services, and financial services.

Jaime Buelta自2010年以来一直是Python的专职开发人员,并在PyCon Ireland担任定期发言人。 在过去的二十多年中,他一直是一名专业的程序员,在他的整个职业生涯中,他对许多不同的技术有着丰富的了解。 他开发了适用于各个领域和行业的软件,包括航空航天,网络和通信,工业SCADA系统,视频游戏在线服务以及金融服务。

Editor’s note: Interested in learning more about coding beyond just retrieving webpages through Python? Check out some of these upcoming similar ODSC talks:

编者注:除了通过Python检索网页之外,您还想了解更多有关编码的信息吗? 查看以下即将举行的类似ODSC讲座:

ODSC Europe: “Programming with Data: Python and Pandas” — In this training, you will learn how to accelerate your data analyses using the Python language and Pandas, a library specifically designed for tabular data analysis.

ODSC欧洲:“ 使用数据编程:Python和Pandas ” —在本培训中,您将学习如何使用Python语言和Pandas(专门用于表格数据分析的库)来加速数据分析。

ODSC Europe: “Introduction to Linear Algebra for Data Science and Machine Learning With Python” — The goal of this session is to show you that you can start learning the math needed for machine learning and data science using code.

ODSC欧洲:“ 使用Python进行数据科学和机器学习的线性代数简介 ” —本课程的目的是向您展示您可以开始使用代码学习机器学习和数据科学所需的数学。

翻译自: https://medium.com/@ODSC/retrieving-webpages-through-python-programming-8f3bae8518a5

python 网页编程


http://www.taodudu.cc/news/show-995064.html

相关文章:

  • data studio_面向营销人员的Data Studio —报表指南
  • 乐高ev3 读取外部数据_数据就是新乐高
  • java 分裂数字_分裂的补充:超越数字,打印物理可视化
  • 比赛,幸福度_幸福与生活满意度
  • 5分钟内完成胸部CT扫描机器学习
  • openai-gpt_为什么到处都看到GPT-3?
  • 数据可视化及其重要性:Python
  • ai驱动数据安全治理_AI驱动的Web数据收集解决方案的新起点
  • 使用K-Means对美因河畔法兰克福的社区进行聚类
  • 因果关系和相关关系 大数据_数据科学中的相关性与因果关系
  • 分类结果可视化python_可视化分类结果的另一种方法
  • rstudio 管道符号_R中的管道指南
  • 时间序列因果关系_分析具有因果关系的时间序列干预:货币波动
  • 无法从套接字中获取更多数据_数据科学中应引起更多关注的一个组成部分
  • 深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景
  • 数据中台是下一代大数据_全栈数据科学:下一代数据科学家群体
  • 泰坦尼克数据集预测分析_探索性数据分析-泰坦尼克号数据集案例研究(第二部分)
  • 大数据技术 学习之旅_如何开始您的数据科学之旅?
  • 搜索引擎优化学习原理_如何使用数据科学原理来改善您的搜索引擎优化工作
  • 一件登录facebook_我从Facebook的R教学中学到的6件事
  • python 图表_使用Streamlit-Python将动画图表添加到仪表板
  • Lockdown Wheelie项目
  • 实现klib_使用klib加速数据清理和预处理
  • 简明易懂的c#入门指南_统计假设检验的简明指南
  • python 工具箱_Python交易工具箱:通过指标子图增强图表
  • python交互式和文件式_使用Python创建和自动化交互式仪表盘
  • 无向图g的邻接矩阵一定是_矩阵是图
  • 熊猫分发_熊猫新手:第一部分
  • 队列的链式存储结构及其实现_了解队列数据结构及其实现
  • 水文分析提取河网_基于图的河网段地理信息分析排序算法

python 网页编程_通过Python编程检索网页相关推荐

  1. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. 使用python开发网页游戏_四大游戏编程网站,边玩游戏,边学Python,拒绝枯燥快乐编程...

    原标题:四大游戏编程网站,边玩游戏,边学Python,拒绝枯燥快乐编程 前言 学习编程虽然对有些人来说是件乐事,但是对大多数人来说仍然是一件比较枯燥困难的事情.当然,面临这样困惑的人,并不是只有你一个 ...

  4. python异步教程_【Python 异步编程入门】

    本文是写给 JavaScript 程序员的 Python 教程. Python 的异步编程,其他人可能觉得很难,但是 JavaScript 程序员应该特别容易理解,因为两者的概念和语法类似.JavaS ...

  5. python环境搭建_搭建Python编程环境

    说明:本文面向信息技术新教材环境下的中学生或者编程小白.Python作为新教材落实计算思维的主要载体,有其独特的魅力.千里之行始于足下,搭建Python运行环境是跨出进入新世界大门的第一步. 搭建Py ...

  6. 自学python需要什么_自学Python编程有什么要求

    现在Python这门课程在计算机专业中特别火爆,我们也都知道,它是时代的趋势,那么大家知道自学Python课程有什么方法或者捷径吗?了解一下 我们首先来看看别人的学习方法吧. 1.看网上名师的教程,学 ...

  7. 用python学编程_用Python学编程

    第1部分 引 论 第1章 关于本书 1.1 什么人要学编程 1.2 本书的内容 1.3 为什么选择Python 1.4 如何阅读本书 1.5 本书内容的组织 第2章 学习编程的要求 2.1 关于编程者 ...

  8. python语言 行业_如何入门编程开发行业 选择Python语言怎么样

    如何入门编程开发行业?选择Python语言怎么样?Python是一种面向对象的解释型计算机程序设计语言,它是纯粹的自由软件,语法简洁清晰,它具有丰富和强大的库.它常被称为胶水语言,能够把用其他语言制作 ...

  9. python2异步编程_最新Python异步编程详解

    我们都知道对于I/O相关的程序来说,异步编程可以大幅度的提高系统的吞吐量,因为在某个I/O操作的读写过程中,系统可以先去处理其它的操作(通常是其它的I/O操作),那么Python中是如何实现异步编程的 ...

最新文章

  1. 多个CALayer的联动
  2. 机器学习理论导引 线上阅读
  3. 揭密Oracle之 七种武器
  4. tensorflow分布式训练之同步更新和异步更新
  5. boost::pfr::get相关的测试程序
  6. IPC RFC call in Service Order scenario
  7. Q145: 三次曲线对比及其矩阵表示(Bezier, B-Spline, Hermite, Catmull-Rom)
  8. 精选| 2019年7月R新包推荐(第32期)
  9. QT 弹出pdf 或者网页【软件 help/about按钮】
  10. (18)全民小视频引流脚本模块化开发12-任务总数与时间间隔By飞云脚本学院
  11. 虚拟机服务器渗透,对一台虚拟主机服务器的渗透 -电脑资料
  12. 图片报道:2008年12月4日夜晚,暴风雪突袭烟台(下)
  13. 用css3 3d效果做一个立体盒子
  14. win7记事本如何转换html,Win7打开记事本显示乱码是为什么?怎么才能正常?
  15. python自动化交易_用Python寫自動交易程式的入門平台: Quantopian
  16. 自动化学报latex模板下载
  17. 标准库intrins.h中的循环指令在多种流水灯方式上的应用
  18. 从零开始成为优秀交互设计师应该怎么做(下)
  19. excel导入mysql 截断_解决Excel导入数据库时出现的文本截断问题
  20. 【概念】FISCO-BCOS证书进行Dapp开发需要注意什么?讲解config.ini,group.id.genesis,group.id.ini

热门文章

  1. Linux编程手册读书笔记第二章(20140330)
  2. HDU5391威尔逊定理
  3. [C++基础]034_C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
  4. TCP socket心跳包示例程序
  5. Linux串口阻塞与非阻塞
  6. 关于对象的引用作为参数,可以直接访问私有成员的问题
  7. 【工作经验分享】这些新技术你们都知道吗
  8. 父、子页面之间页面元素的获取,方法的调用
  9. windows查看系统版本号
  10. BZOJ2597 WC2007剪刀石头布(费用流)