Python中的网络爬虫库获取网页的示例程序是什么?

优采云 发布时间: 2021-07-13 21:17

  Python中的网络爬虫库获取网页的示例程序是什么?

  1 什么是网络爬虫

  网络爬虫是指从网站中提取数据的技术,可以将非结构化数据转化为结构化数据。

  网络爬虫的目的是从网站中提取数据。提取的数据可以存储在本地文件中并保存在系统中,也可以以表格的形式存储在数据库中。网络爬虫使用 HTTP 或网络浏览器直接访问万维网 (WWW)。网络爬虫或机器人抓取网页的过程是一个自动化过程。

  抓取网页的过程分为网页获取和数据提取。网络爬虫可以抓取网页,是网络爬虫的必备组件。获取网页后,需要提取网页数据。我们可以对提取的数据进行搜索、解析,并将其保存在表格中,然后重新排列格式。

  2 数据提取

  在本节中,我们将了解数据提取。我们可以使用 Python 的 BeautifulSoup 库进行数据提取。这里还需要 Python 库的 Requests 模块。

  运行以下命令来安装 Requests 和 BeautifulSoup 库。

  $ pip3 install requests

$ pip3 install beautifulsoup4

  2.1Requests 库

  使用请求库以易于理解的格式在 Python 脚本中使用 HTTP。在这里,使用 Python 中的 Requests 库来获取网页。 Requests 库收录不同类型的请求,这里使用 GET 请求。 GET请求用于从Web服务器获取信息,通过GET请求可以获取指定网页的HTML内容。每个请求对应一个状态码,从服务器返回。这些状态码为我们提供了相应请求执行结果的相关信息。以下是一些状态代码。

  2.2BeautifulSoup 库

  BeautifulSoup 也是一个 Python 库,收录简单的搜索、导航和修改方法。它只是一个从网页中提取所需数据的工具包。

  要在脚本中使用 Requests 和 BeautifulSoup 模块,必须使用 import 语句导入这两个模块。现在让我们看一个用于解析网页的示例程序。在这里,我们将解析来自百度网站 的新闻网页。创建一个脚本,命名为parse_web_page.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 对其进行了解析。首先导入requests和BeautifulSoup模块,然后使用GET请求访问URL,并将结果赋值给page_result变量,然后创建一个BeautifulSoup对象parse_obj,将requests page_result.content的返回结果作为参数,然后用 html.parser 解析了这个页面。

  现在我们将从类和标签中提取数据。进入网页浏览器,右击要提取的内容向下搜索,找到“勾选”选项,点击获取类名。在程序中指定这个类名并运行脚本。创建一个脚本,将其命名为extract_from_class.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,

  上面的示例程序首先导入 requests 和 BeautifulSoup 模块,然后创建一个 requests 对象并为其分配一个 URL,然后创建一个 BeautifulSoup 对象 parse_obj。该对象以请求的返回结果 page_result.content 为参数,然后使用 html.parser 解析页面。最后,使用 BeautifulSoup 的 find() 方法从 news-article__content 类中获取内容。

  现在让我们看一个从特定标签中提取数据的示例程序。此示例程序将从标签中提取数据。创建一个脚本,将其命名为extract_from_tag.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]

  上面的示例程序从标签中提取数据。这里我们使用 find_all() 方法从 news-article__content 类中提取所有标签数据。

  3 从 Wikipedia网站 获取信息

  在本节中,我们将学习一个示例程序,用于从维基百科网站 获取舞蹈类型列表。在这里,我们将列出所有经典的印度舞蹈。创建一个脚本,将其命名为extract_from_wikipedia.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

  输出如下。

  

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.

....

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线