$ pip3 install requests $ pip3 install beautifulsoup4
2.1 Requests库 使用Requests库可以易懂的格式在Python脚本中使用HTTP,这里使用Python中的Requests库获取网页。Requests库包含不同类型的请求,这里使用GET请求。GET请求用于从Web服务器获取信息,使用GET请求可以获取指定网页的HTML内容。每个请求都对应一个状态码,状态码从服务器返回,这些状态码为我们提供了对应请求执行结果的相关信息。以下是部分状态码。
301:表示如果服务器已切换域名或必须更改端点名称,则服务器将重定向到其他端点。
400:表示用户发出了错误请求。
401:表示用户未通过身份验证。
403:表示用户正在尝试访问禁用的资源。
404:表示用户尝试访问的资源在服务器上不可用。
2.2 BeautifulSoup库 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:<!DOCTYPE html> <html xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://ogp.me/ns#"> <head> <meta charset="utf-8"/> <meta content="IE=edge" http-equiv="X-UA-Compatible"/> <meta content="app-id=342792525, app-argument=imdb:///?src=mdot" name="apple-itunes-app"/> <script type="text/javascript">var IMDbTimer={starttime: new Date().getTime(),pt:'java'};</script> <script> if (typeof uet == 'function') { uet("bb", "LoadTitle", {wb: 1}); } </script> <script>(function(t){ (t.events = t.events || {})["csm_head_pre_title"] = new Date().getTime(); })(IMDbTimer);</script> <title>Top News - IMDb</title> <script>(function(t){ (t.events = t.events || {})["csm_head_post_title"] = new Date().getTime(); })(IMDbTimer);</script> <script> if (typeof uet == 'function') { uet("be", "LoadTitle", {wb: 1}); } </script> <script> if (typeof uex == 'function') { uex("ld", "LoadTitle", {wb: 1}); } </script> <link href="https://www.imdb.com/news/top" rel="canonical"/> <meta content="http://www.imdb.com/news/top" property="og:url"> <script> 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解析该页面。 现在我们将从类和标签中提取数据。转到Web浏览器,右击要提取的内容并向下查找,找到“检查”选项,单击它将获得类名。在程序中指定这个类名,并运行脚本。创建一个脚本,命名为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 :<div class="news-article__content"> <a href="/name/nm4793987/">Issa Rae</a> and <a href="/name/nm0000368/">Laura Dern</a> are teaming up to star in a limited series called "The Dolls" currently in development at <a href="/company/co0700043/">HBO</a>.<br/><br/>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."<br/><br/>Rae will serve as a writer and executive producer on the series in addition to starring, with Dern also executive producing. <a href="/name/nm3308450/">Laura Kittrell</a> and <a href="/name/nm4276354/">Amy Aniobi</a> will also serve as writers and coexecutive producers. <a href="/name/nm0501536/">Jayme Lemons</a> of Dern’s <a href="/company/co0641481/">Jaywalker Pictures</a> and <a href="/name/nm3973260/">Deniese Davis</a> of <a href="/company/co0363033/">Issa Rae Productions</a> will also executive produce.<br/><br/>Both Rae and Dern currently star in HBO shows, with Dern appearing in the acclaimed drama "<a href="/title/tt3920596/">Big Little Lies</a>" and Rae starring in and having created the hit comedy "<a href="/title/tt5024912/">Insecure</a>." Dern also recently starred in the film "<a href="/title/tt4015500/">The Tale</a>, </div>
上面的示例程序首先导入了requests和BeautifulSoup模块,然后创建了一个requests对象并为其分配了一个URL,接着创建了一个BeautifulSoup对象parse_obj。此对象将requests的返回结果page_result.content作为参数,然后使用html.parser解析页面。最后,使用BeautifulSoup的find()方法从news-article__content类中获取内容。 现在我们来看一个从特定标签中提取数据的示例程序,此示例程序将从<a>标签中提取数据。创建一个脚本,命名为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:[<a href="/name/nm4793987/">Issa Rae</a>, <a href="/name/nm0000368/">Laura Dern</a>, <a href="/company/co0700043/">HBO</a>, <a href="/name/nm3308450/">Laura Kittrell</a>, <a href="/name/nm4276354/">Amy Aniobi</a>, <a href="/name/nm0501536/">Jayme Lemons</a>, <a href="/company/co0641481/">Jaywalker Pictures</a>, <a href="/name/nm3973260/">Deniese Davis</a>, <a href="/company/co0363033/">Issa Rae Productions</a>, <a href="/title/tt3920596/">Big Little Lies</a>, <a href="/title/tt5024912/">Insecure</a>, <a href="/title/tt4015500/">The Tale</a>]
上面的示例程序从<a>标签中提取数据。这里使用find_all()方法从news-article__content类中提取所有<a>标签数据。 3 从*网站抓取信息 本节我们将学习一个从*网站获取舞蹈种类列表的示例程序,这里将列出所有古典印度舞蹈。创建一个脚本,命名为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 输出如下。 <div class="hlist noprint" id="portals-browsebar" style="text-align: center;"> <dl><dt><a href="/wiki/Portal:Contents/Portals" title="Portal:Contents/Portals">Portal topics</a></dt> <dd><a href="/wiki/Portal:Contents/Portals#Human_activities" title="Portal:Contents/Portals">Activities</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#Culture_and_the_arts" title="Portal:Contents/Portals">Culture</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#Geography_and_places" title="Portal:Contents/Portals">Geography</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#Health_and_fitness" title="Portal:Contents/Portals">Health</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#History_and_events" title="Portal:Contents/Portals">History</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#Mathematics_and_logic" title="Portal:Contents/Portals">Mathematics</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#Natural_and_physical_sciences" title="Portal:Contents/Portals">Nature</a></dd> <dd><a href="/wiki/Portal:Contents/Portals#People_and_self" title="Portal:Contents/Portals">People</a></dd> In the preceding example, we extracted the content from Wikipedia. In this example also, we extracted the content from class as well as tag. ....