在本章中,我们将学习网络爬虫,包括正在研究的库,它用于从网站中提取数据。
本章包含以下主题。
1 什么是网络爬虫
网络爬虫是指从网站中提取数据的技术,将非结构化数据转换为结构化数据。
网络爬虫的目的是从网站中提取数据。 提取的数据可以存储在本地文件中并保存在系统中,也可以以表格的形式存储在数据库中。 网络爬虫直接使用 HTTP 或网络浏览器访问万维网 (WWW)。 网络爬虫或机器人抓取网络的过程是一个自动化过程。
抓取网页的过程分为获取网页和提取数据。 网络爬虫获取网页,是网络爬虫的必需组件。 获取网页后,需要提取网页数据。 我们可以搜索、解析提取的数据并将其保存到表中并重新格式化。
2 数据提取
在本节中,我们将学习数据提取。 我们可用于数据提取的库。 这里还需要库的模块。
运行以下命令来安装和库。
$ pip3 install requests
$ pip3 install beautifulsoup4
2. 图书馆
使用该库以人类可读的格式在脚本中使用 HTTP,此处使用的库获取网页。 该库包含不同类型的请求,这里使用 GET 请求。 GET请求用于从Web服务器获取信息,通过GET请求可以获取指定网页的HTML内容。 每个请求对应一个状态码,状态码是从服务器返回的。 这些状态码为我们提供了相应请求的执行结果信息。 以下是一些状态代码。
2. 图书馆
还有一个库,其中包含用于搜索、导航和修改的简单方法。 它只是一个从网页中提取所需数据的工具包。
要在脚本中使用 和 模块,必须使用该语句导入这两个模块。 现在我们来看一个解析网页的示例程序,这里我们将解析来自百度网站的新闻网页。 创建一个脚本,将其命名为.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:
ta charset="utf-8"/>
ta content="IE=edge" http-equiv="X-UA-Compatible"/>
ta content="app-id=342792525, app-argument=imdb:///?src=mdot"
name="apple-itunes-app"/>
cript type="text/javascript">var IMDbTimer={starttime: new
Date().getTime(),pt:'java'};cript>
cript>
if (typeof uet == 'function') {
uet("bb", "LoadTitle", {wb: 1});
}
cript>
cript>(function(t){ (t.events = t.events || {})["csm_head_pre_title"] =
new Date().getTime(); })(IMDbTimer);cript>
Top News - IMDb
cript>(function(t){ (t.events = t.events || {})["csm_head_post_title"] =
new Date().getTime(); })(IMDbTimer);cript>
cript>
if (typeof uet == 'function') {
uet("be", "LoadTitle", {wb: 1});
}
cript>
cript>
if (typeof uex == 'function') {
uex("ld", "LoadTitle", {wb: 1});
}
cript>
ink href="https://www.imdb.com/news/top" rel="canonical"/>
ta content="http://www.imdb.com/news/top" property="og:url">
cript>
if (typeof uet == 'function') {
uet("bb", "LoadIcons", {wb: 1});
}
上面的示例程序抓取一个网页并使用 . 首先导入 and 模块,然后使用 GET 请求访问 URL,并将结果分配给变量,然后创建一个将返回结果的对象。 作为参数,然后使用html。 来解析页面。
现在我们将从类和标签中提取数据。 转到您的网络浏览器,右键单击要提取的内容并向下查找,找到“”选项,单击它,您将获得类名称。 在程序中指定此类名称并运行脚本。 创建一个脚本,将其命名为.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 Arkansas
towns in 1983, riots which erupted over Cabbage Patch Dolls. The series
explores class, race, privilege and what it takes to be a "good
mother."
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,
上面的示例程序首先导入 and 模块,然后创建一个对象并为其分配一个 URL,然后创建一个对象。 该对象将返回结果。 作为参数,然后使用html。 来解析页面。 最后,使用 find() 方法从新闻类中获取内容。
现在让我们看一个从特定标签中提取数据的示例程序,该示例程序将从标签中提取数据。 创建一个脚本,将其命名为.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]
上面的示例程序从标签中提取数据。 这里 () 方法用于从 news-class 中提取所有标签数据。
3 从维基百科网站抓取信息
在本节中,我们将学习一个示例程序,用于从维基百科网站获取舞蹈类别列表,这里我们将列出所有古典印度舞蹈。 创建一个脚本,将其命名为ia.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 extract_from_wikipedia.py
输出如下。