LIST
파이썬 튜토리얼에서 코드만 뽑아보기 실습을 해보겠습니다. BeautifulSoup 을 사용하여 정보를 추출하였습니다. BeautifulSoup은 텍스트형태의 html에서 원하는 html 태그를 추출합니다. |
1. BeautifulSoup 설치
pip install beautifulsoup4
2. BeautifulSoup 사용
# -*- coding: utf-8 -*-
import requests # requests 모듈을 불러옵니다.
url = "https://docs.python.org/3/tutorial/stdlib2.html" # 크롤링할 url 주소를 입력합니다.
response = requests.get(url) # HTTP 요청을 보내는 모듈인 requests
html_text = response.text
from bs4 import BeautifulSoup as bs # BeautifulSoup을 사용하여 텍스트 추출하고 BeautifulSoup을 bs 로 정의함
html = bs(html_text, 'html.parser') #html.parser은 BeautifulSoup에 존재하는 parser의 종류 중 하나로, 일반적으로 많이 사용함
soup = bs(response.text,'html.parser')
text_titles = soup.select("p") # p 태그를 찾는다.
for i in text_titles: # p태그를 찾아서 title에 입력해주고 그 값을 출력한다.
title = i.get_text() #text만 뽑아 온다.
print(title)
출력
10. Brief Tour of the Standard Library
12. Virtual Environments and Packages
This second tour covers more advanced modules that support professional
programming needs. These modules rarely occur in small scripts.
The reprlib module provides a version of repr() customized for
abbreviated displays of large or deeply nested containers:
The pprint module offers more sophisticated control over printing both
built-in and user defined objects in a way that is readable by the interpreter.
When the result is longer than one line, the âpretty printerâ adds line breaks
and indentation to more clearly reveal data structure:
The textwrap module formats paragraphs of text to fit a given screen
width:
The locale module accesses a database of culture specific data formats.
The grouping attribute of localeâs format function provides a direct way of
formatting numbers with group separators:
The string module includes a versatile Template class
with a simplified syntax suitable for editing by end-users. This allows users
to customize their applications without having to alter the application.
The format uses placeholder names formed by $ with valid Python identifiers
(alphanumeric characters and underscores). Surrounding the placeholder with
braces allows it to be followed by more alphanumeric letters with no intervening
spaces. Writing $$ creates a single escaped $:
The substitute() method raises a KeyError when a
placeholder is not supplied in a dictionary or a keyword argument. For
mail-merge style applications, user supplied data may be incomplete and the
safe_substitute() method may be more appropriate â
it will leave placeholders unchanged if data is missing:
Template subclasses can specify a custom delimiter. For example, a batch
renaming utility for a photo browser may elect to use percent signs for
placeholders such as the current date, image sequence number, or file format:
Another application for templating is separating program logic from the details
of multiple output formats. This makes it possible to substitute custom
templates for XML files, plain text reports, and HTML web reports.
The struct module provides pack() and
unpack() functions for working with variable length binary
record formats. The following example shows
how to loop through header information in a ZIP file without using the
zipfile module. Pack codes "H" and "I" represent two and four
byte unsigned numbers respectively. The "<" indicates that they are
standard size and in little-endian byte order:
Threading is a technique for decoupling tasks which are not sequentially
dependent. Threads can be used to improve the responsiveness of applications
that accept user input while other tasks run in the background. A related use
case is running I/O in parallel with computations in another thread.
The following code shows how the high level threading module can run
tasks in background while the main program continues to run:
The principal challenge of multi-threaded applications is coordinating threads
that share data or other resources. To that end, the threading module provides
a number of synchronization primitives including locks, events, condition
variables, and semaphores.
While those tools are powerful, minor design errors can result in problems that
are difficult to reproduce. So, the preferred approach to task coordination is
to concentrate all access to a resource in a single thread and then use the
queue module to feed that thread with requests from other threads.
Applications using Queue objects for inter-thread communication and
coordination are easier to design, more readable, and more reliable.
The logging module offers a full featured and flexible logging system.
At its simplest, log messages are sent to a file or to sys.stderr:
This produces the following output:
By default, informational and debugging messages are suppressed and the output
is sent to standard error. Other output options include routing messages
through email, datagrams, sockets, or to an HTTP Server. New filters can select
different routing based on message priority: DEBUG,
INFO, WARNING, ERROR,
and CRITICAL.
The logging system can be configured directly from Python or can be loaded from
a user editable configuration file for customized logging without altering the
application.
Python does automatic memory management (reference counting for most objects and
garbage collection to eliminate cycles). The memory is freed shortly
after the last reference to it has been eliminated.
This approach works fine for most applications but occasionally there is a need
to track objects only as long as they are being used by something else.
Unfortunately, just tracking them creates a reference that makes them permanent.
The weakref module provides tools for tracking objects without creating a
reference. When the object is no longer needed, it is automatically removed
from a weakref table and a callback is triggered for weakref objects. Typical
applications include caching objects that are expensive to create:
Many data structure needs can be met with the built-in list type. However,
sometimes there is a need for alternative implementations with different
performance trade-offs.
The array module provides an array() object that is like
a list that stores only homogeneous data and stores it more compactly. The
following example shows an array of numbers stored as two byte unsigned binary
numbers (typecode "H") rather than the usual 16 bytes per entry for regular
lists of Python int objects:
The collections module provides a deque() object
that is like a list with faster appends and pops from the left side but slower
lookups in the middle. These objects are well suited for implementing queues
and breadth first tree searches:
In addition to alternative list implementations, the library also offers other
tools such as the bisect module with functions for manipulating sorted
lists:
The heapq module provides functions for implementing heaps based on
regular lists. The lowest valued entry is always kept at position zero. This
is useful for applications which repeatedly access the smallest element but do
not want to run a full list sort:
The decimal module offers a Decimal datatype for
decimal floating point arithmetic. Compared to the built-in float
implementation of binary floating point, the class is especially helpful for
financial applications and other uses which require exact decimal
representation,
control over precision,
control over rounding to meet legal or regulatory requirements,
tracking of significant decimal places, or
applications where the user expects the results to match calculations done by
hand.
For example, calculating a 5% tax on a 70 cent phone charge gives different
results in decimal floating point and binary floating point. The difference
becomes significant if the results are rounded to the nearest cent:
The Decimal result keeps a trailing zero, automatically
inferring four place significance from multiplicands with two place
significance. Decimal reproduces mathematics as done by hand and avoids
issues that can arise when binary floating point cannot exactly represent
decimal quantities.
Exact representation enables the Decimal class to perform
modulo calculations and equality tests that are unsuitable for binary floating
point:
The decimal module provides arithmetic with as much precision as needed:
10. Brief Tour of the Standard Library
12. Virtual Environments and Packages
Process finished with exit code 0
반응형
LIST
'파이썬' 카테고리의 다른 글
4-1. IF 문 (0) | 2022.10.20 |
---|---|
[실습] 파이썬 튜토리얼에서 코드만 뽑아오기 2 [BeatifulSoup] (0) | 2022.09.04 |
11. 표준 라이브러리 -2 (0) | 2022.08.21 |
10. 표준 라이브러리 (0) | 2022.08.21 |
8. 예외와 에러 (0) | 2022.08.05 |
댓글