网页数据抓取软件(网络爬虫或机器人解析网页的示例程序解析库 )
优采云 发布时间: 2021-09-19 16:22网页数据抓取软件(网络爬虫或机器人解析网页的示例程序解析库
)
1什么是网络爬虫
网络爬虫是指网站提取数据的技术,它可以将非结构化数据转换为结构化数据
网络爬虫的目的是从网络中提取数据网站. 提取的数据可以存储在本地文件中并保存在系统中,或者以表的形式存储在数据库中。网络爬虫使用HTTP或Web浏览器直接访问万维网(WWW)。网络爬虫或机器人抓取网页的过程是一个自动过程
捕获网页的过程分为获取网页和提取数据。网络爬虫能够获取网页,是网络爬虫的必要组成部分。获取网页后,需要提取网页数据。我们可以搜索、解析并将提取的数据保存到表中,然后重新排列格式
2数据提取
在本节中,我们将研究数据提取。我们可以使用Python漂亮的汤库进行数据提取。您还需要使用python库的requests模块
运行以下命令来安装请求和库
$ pip3 install requests
$ pip3 install beautifulsoup4
2.1Requests仓库
使用请求库以易于理解的格式在Python脚本中使用HTTP。这里,使用Python中的请求库获取web页面。请求库收录不同类型的请求。这里使用get请求。Get请求用于从web服务器获取信息。使用get请求获取指定网页的HTML内容。每个请求对应一个状态代码,该代码从服务器返回。这些状态代码为我们提供了有关相应请求的执行结果的相关信息。这里有一些状态代码
2.2BeautifulSoup仓库
Beauty soup也是一个python库,收录简单的搜索、导航和修改方法。它只是一个从网页中提取所需数据的工具包
要在脚本中使用请求和模块,必须使用import语句导入它们。现在让我们看一个解析网页的示例程序。这里我们将解析一个来自百度的新闻网页网站. 创建一个名为parse_uWeb_uuPage.py的脚本,并在其中编写以下代码
import requests
from bs4 import BeautifulSoup
page_result = requests.get('https://www.news.baidu.com')
parse_obj = BeautifulSoup(page_result.content, 'html.parser')
print(parse_obj)
运行脚本程序,如下所示。
student@ubuntu:~/work$ python3 parse_web_page.py
Output:
var IMDbTimer={starttime: new
Date().getTime(),pt:'java'};
if (typeof uet == 'function') {
uet("bb", "LoadTitle", {wb: 1});
}
(function(t){ (t.events = t.events || {})["csm_head_pre_title"] =
new Date().getTime(); })(IMDbTimer);
Top News - IMDb
(function(t){ (t.events = t.events || {})["csm_head_post_title"] =
new Date().getTime(); })(IMDbTimer);
if (typeof uet == 'function') {
uet("be", "LoadTitle", {wb: 1});
}
if (typeof uex == 'function') {
uex("ld", "LoadTitle", {wb: 1});
}
if (typeof uet == 'function') {
uet("bb", "LoadIcons", {wb: 1});
}
上面的示例程序抓取一个网页并用BeautifulSoup解析它。首先,导入请求和beautifulsoup模块,然后使用get请求访问URL并将结果分配给page_uResult变量,然后创建一个beautiful soup对象parse_Obj,该对象将返回requests_Result.content的结果页面作为参数,然后使用html.parser解析页面
现在我们将从类和标记中提取数据。转到web浏览器,右键单击要提取的内容,然后向下查找“检查”选项。单击它以获取类名。在程序中指定类名并运行脚本。创建一个名为extract_uufrom_u类的脚本。Py并在其中编写以下代码
import requests
from bs4 import BeautifulSoup
page_result = requests.get('https://www.news.baidu.com')
parse_obj = BeautifulSoup(page_result.content, 'html.parser')
top_news = parse_obj.find(class_='news-article__content')
print(top_news)
运行脚本程序,如下所示
student@ubuntu:~/work$ python3 extract_from_class.py
Output :
Issa Rae and Laura Dern are teaming up to star in a limited
series called "The Dolls" currently in development at HBO.
Inspired by true events, the
series recounts the aftermath of Christmas Eve riots in two small Arkansastowns in 1983, riots which erupted over Cabbage Patch Dolls. The seriesexplores class, race, privilege and what it takes to be a "goodmother."
Rae will serve as a writer and executive producer on the
series in addition to starring, with Dern also executive producing. Laura Kittrell and Amy Aniobi will also serve as writers and coexecutive
producers. Jayme Lemons of Dern’s
Jaywalker Pictures and Deniese Davis of Issa Rae Productions will also executive
produce.
Both Rae and Dern currently star in HBO shows, with Dern
appearing in the acclaimed drama "Big Little
Lies" and Rae starring in and having created the hit comedy "Insecure." Dern also recently starred in the
film "The Tale,
上面的示例程序首先导入请求和beautulsoup模块,然后创建一个请求对象并为其分配一个URL,然后创建一个beautulsoup对象parse_uuj。该对象将requests_uuresult.content的结果页作为参数返回,然后使用html.parser解析该页。最后,使用beautifulsoup的find()方法从新闻文章中获取信息
现在让我们看一个从特定标记中提取数据的示例程序。此示例程序将从标记中提取数据。创建一个名为extract_ufrom_u标记.py的脚本,并在其中编写以下代码
import requests
from bs4 import BeautifulSoup
page_result = requests.get('https://www.news.baidu.com/news')
parse_obj = BeautifulSoup(page_result.content, 'html.parser')
top_news = parse_obj.find(class_='news-article__content')
top_news_a_content = top_news.find_all('a')
print(top_news_a_content)
运行脚本程序,如下所示
student@ubuntu:~/work$ python3 extract_from_tag.py
Output:[Issa Rae, Laura
Dern, HBO, Laura Kittrell, Amy
Aniobi, Jayme Lemons, Jaywalker Pictures, Deniese Davis, Issa Rae Productions, Big Little Lies, Insecure, The
Tale]
上面的示例程序从标记中提取数据。这里使用的是新闻文章中的\uAll()方法\uuuuu从内容类中提取所有标记数据
3从维基百科网站获取信息@
在本节中,我们将学习一个示例程序网站从维基百科获取舞蹈类别列表。这里我们将列出所有印度古典舞蹈。创建一个名为extract\from_uwikipedia.py的脚本,并在其中编写以下代码
import requests
from bs4 import BeautifulSoup
page_result = requests.get('https://en.wikipedia.org/wiki/Portal:History')
parse_obj = BeautifulSoup(page_result.content, 'html.parser')
h_obj = parse_obj.find(class_='hlist noprint')
h_obj_a_content = h_obj.find_all('a')
print(h_obj)
print(h_obj_a_content)
运行脚本程序,如下所示
student@ubuntu:~/work$python3摘录自wikipedia.py
结果如下
Portal topics
Activities
Culture
Geography
Health
History
Mathematics
Nature
People
In the preceding example, we extracted the content from Wikipedia. In this
example also, we extracted the content from class as well as tag.
....