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

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.
主站蜘蛛池模板: 石阡县| 唐山市| 甘泉县| 交城县| 怀远县| 六枝特区| 包头市| 琼结县| 谷城县| 兴义市| 河东区| 小金县| 济宁市| 托克托县| 长白| 齐河县| 玉龙| 淅川县| 龙泉市| 贺州市| 靖安县| 宾川县| 汾西县| 闽侯县| 即墨市| 许昌市| 兴海县| 中超| 灌云县| 澄江县| 根河市| 新和县| 嘉义县| 乌苏市| 甘泉县| 罗山县| 聊城市| 建平县| 宁阳县| 响水县| 日喀则市|