官术网_书友最值得收藏!

How it works...

We will dive into details of both Requests and Beautiful Soup in the next chapter, but for now let's just summarize a few key points about how this works.  The following important points about Requests:

  • Requests is used to execute HTTP requests.  We used it to make a GET verb request of the URL for the events page.
  • The Requests object holds the results of the request.  This is not only the page content, but also many other items about the result such as HTTP status codes and headers.
  • Requests is used only to get the page, it does not do an parsing.

We use Beautiful Soup to do the parsing of the HTML and also the finding of content within the HTML. 

To understand how this worked, the content of the page has the following HTML to start the Upcoming Events section:

We used the power of Beautiful Soup to:

  • Find the <ul> element representing the section, which is found by looking for a <ul> with the a class attribute that has a value of list-recent-events.
  • From that object, we find all the <li> elements. 

Each of these <li> tags represent a different event.  We iterate over each of those making a dictionary from the event data found in child HTML tags:

  • The name is extracted from the <a> tag that is a child of the <h3> tag
  • The location is the text content of the <span> with a class of event-location
  • And the time is extracted from the datetime attribute of the <time> tag.
主站蜘蛛池模板: 耿马| 涿州市| 正阳县| 宜州市| 靖边县| 二手房| 太康县| 安西县| 建湖县| 高邮市| 波密县| 烟台市| 霍林郭勒市| 志丹县| 福州市| 准格尔旗| 丰镇市| 壤塘县| 博罗县| 青冈县| 高要市| 囊谦县| 永康市| 高州市| 桃园市| 永济市| 太保市| 大连市| 桦南县| 武冈市| 彭阳县| 喀喇沁旗| 嘉兴市| 郸城县| 伊川县| 应城市| 华宁县| 凤山市| 盐津县| 蕉岭县| 衡东县|