6-8, 10,11

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the CSS selector string that would find the element with an id attribute of main?

'#main'

What is the CSS selector string that would find the elements with a CSS class of highlight?

'.highlight'

What do the following expressions evaluate to? 'Hello'.upper() 'Hello'.upper().isupper() 'Hello'.upper().lower()

'HELLO' True 'hello'

If numRegex = re.compile(r'\d+'), what will numRegex.sub('X', '12 drummers, 11 pipers, five rings, 3 hens') return?

'X drummers, X pipers, five rings, X hens'

What is the CSS selector string that would find the <button> element with a value attribute set to favorite?

'button[value="favorite"]'

What is the CSS selector string that would find all the <div> elements inside another <div> element?

'div div'

What do the following expressions evaluate to? 'Hello world!'[1] 'Hello world!'[0:5] 'Hello world!'[:5] 'Hello world!'[3:]

'e' 'Hello' 'Hello' 'lo world!

What is a breakpoint?

A breakpoint is a setting on a line of code that causes the debugger to pause when the program execution reaches the line.

What data structure does a shelf value resemble?

A shelf value resembles a dictionary value; it has keys and values, along with keys() and values() methods that work similarly to the dictionary methods of the same names.

What does an absolute path start with?

Absolute paths start with the root folder, such as / or C:\.

How do you save a Requests response to a file?

After opening the new file on your computer in 'wb' "write binary" mode, use a for loop that iterates over the Response object's iter_content() method to write out chunks to the file. Here's an example: saveFile = open('filename.html', 'wb') for chunk in res.iter_content(100000): saveFile.write(chunk)

After you click Go in the Debug Control window, when will the debugger stop?

After you click Go, the debugger will stop when it has reached the end of the program or a line with a breakpoint.

What happens if an existing file is opened in write mode?

An existing file opened in write mode is erased and completely overwritten.

In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name?

C:\bacon\eggs is the dir name, while spam.txt is the base name.

You could call send_keys(Keys.ENTER) on the Submit button's WebElement object, but what is an easier way to submit a form with Selenium?

Calling the submit() method on any element within a form submits the form.

What are the five logging levels?

DEBUG, INFO, WARNING, ERROR, and CRITICAL

What is the character class syntax to match all numbers and lowercase letters?

Either [0-9a-z] or [a-z0-9]

What are escape characters?

Escape characters represent characters in string values that would otherwise be difficult or impossible to type into code.

What is the keyboard shortcut for opening a browser's developer tools?

F12 brings up the developer tools in Chrome. Pressing CTRL-SHIFT-C (on Windows and Linux) or ⌘-OPTION-C (on OS X) brings up the developer tools in Firefox.

In the regex created from r'(\d\d\d)-(\d\d\d-\d\d\d\d)', what does group 0 cover? Group 1? Group 2?

Group 0 is the entire match, group 1 covers the first set of parentheses, and group 2 covers the second set of parentheses.

The findall() method returns a list of strings or a list of tuples of strings. What makes it return one or the other?

If the regex has no groups, a list of strings is returned. If the regex has groups, a list of tuples of strings is returned.

If you don't want to put \n in your string, how can you write a string with newlines in it?

Multiline strings allow you to use newlines in strings without the \n escape character.

How do you make a regular expression case-insensitive?

Passing re.I or re.IGNORECASE as the second argument to re.compile() will make the matching case insensitive.

Parentheses and periods have specific meanings in regular expression syntax. How would you specify that you want a regex to match actual parentheses and period characters?

Periods and parentheses can be escaped with a backslash: \., \(, and \).

Why are raw strings often used when creating Regex objects?

Raw strings are used so that backslashes do not have to be escaped.

What is a relative path relative to?

Relative paths are relative to the current working directory.

How can you view (in the developer tools) the HTML of a specific element on a web page?

Right-click the element in the page, and select Inspect Element from the menu.

What is the difference between the + and * characters in regular expressions?

The + matches one or more. The * matches zero or more.

What does the . character normally match? What does it match if re.DOTALL is passed as the second argument to re.compile()?

The . character normally matches any character except the newline character. If re.DOTALL is passed as the second argument to re.compile(), then the dot will also match newline characters.

What are the . and .. folders?

The . folder is the current folder, and .. is the parent folder.

What is the difference between these two: .* and .*?

The .* performs a greedy match, and the .*? performs a nongreedy match.

What two things does the ? character signify in regular expressions?

The ? character can either mean "match zero or one of the preceding group" or be used to signify nongreedy matching.

What are the differences between the Step, Over, and Out buttons in the Debug Control window?

The Step button will move the debugger into a function call. The Over button will quickly execute the function call without stepping into it. The Out button will quickly execute the rest of the code until it steps out of the function it currently is in.

What do the \D, \W, and \S shorthand character classes signify in regular expressions?

The \D, \W, and \S shorthand character classes match a single character that is not a digit, word, or space character, respectively.

How can you put a \ backslash character in a string?

The \\ escape character will represent a backslash character.

What do the \d, \w, and \s shorthand character classes signify in regular expressions?

The \d, \w, and \s shorthand character classes match a single digit, word, or space character, respectively.

What methods do Selenium's WebElement objects have for simulating mouse clicks and keyboard keys?

The click() and send_keys() methods simulate mouse clicks and keyboard keys, respectively.

What's the difference between the find_element_* and find_elements_* methods?

The find_element_* methods return the first matching element as a WebElement object. The find_elements_* methods return a list of all matching elements as WebElement objects.

How can you simulate clicking a browser's Forward, Back, and Refresh buttons with Selenium?

The forward(), back(), and refresh() WebDriver object methods simulate these browser buttons.

How do you get the actual strings that match the pattern from a Match object?

The group() method returns strings of the matched text.

How can you trim whitespace characters from the beginning or end of a string?

The lstrip() and rstrip() methods remove whitespace from the left and right ends of a string, respectively.

What do the os.getcwd() and os.chdir() functions do?

The os.getcwd() function returns the current working directory. The os.chdir() function changes the current working directory.

What Requests method checks that the download worked?

The raise_for_status() method raises an exception if the download had problems and does nothing if the download succeeded.

What does passing re.VERBOSE as the second argument to re.compile() allow you to do?

The re.VERBOSE argument allows you to add whitespace and comments to the string passed to re.compile().

What is the function that creates Regex objects?

The re.compile() function returns Regex objects.

What is the difference between the read() and readlines() methods?

The read() method returns the file's entire contents as a single string value. The readlines() method returns a list of strings, where each string is a line from the file's contents.

What type of object is returned by requests.get()? How can you access the downloaded content as a string value?

The requests.get() function returns a Response object, which has a text attribute that contains the downloaded content as a string.

What string methods can you use to right-justify, left-justify, and center a string?

The rjust(), ljust(), and center() string methods, respectively

What does the search() method return?

The search() method returns Match objects.

Running import selenium doesn't work. How do you properly import the selenium module?

The selenium module is imported with from selenium import webdriver.

The string value "Howl's Moving Castle" is a valid string. Why isn't is a problem that the single quote character in the word Howl's isn't escaped?

The single quote in Howl's is fine because you've used double quotes to mark the beginning and end of the string.

How can you get the HTTP status code of a Requests response?

The status_code attribute of the Response object contains the HTTP status code.

What are the three "mode" arguments that can be passed to the open() function?

The string 'r' for read mode, 'w' for write mode, and 'a' for append mode

Briefly describe the differences between the webbrowser, requests, BeautifulSoup, and selenium modules.

The webbrowser module has an open() method that will launch a web browser to a specific URL, and that's it. The requests module can download files and pages from the Web. The BeautifulSoup module parses HTML. Finally, the selenium module can launch and control a browser.

What is the difference between {3} and {3,5} in regular expressions?

The {3} matches exactly three instances of the preceding group. The {3,5} matches between three and five instances.

What does the | character signify in regular expressions?

The | character signifies matching "either, or" between two groups.

What are the two lines that your program must have in order to be able to call logging.debug()?

To be able to call logging.debug(), you must have these two lines at the start of your program: import logging logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

What are the two lines that your program must have in order to have logging.debug() send a logging message to a file named programLog.txt?

To be able to send logging messages to a file named programLog.txt with logging.debug(), you must have these two lines at the start of your program: import logging >>> logging.basicConfig(filename='programLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

How do you set a breakpoint on a line of code in IDLE?

To set a breakpoint in IDLE, right-click the line and select Set Breakpoint from the context menu.

Why is using logging messages better than using print() to display the same message?

You can disable logging messages without removing the logging function calls. You can selectively disable lower-level logging messages. You can create logging messages. Logging messages provides a timestamp.

What do the following expressions evaluate to? 'Remember, remember, the fifth of November.'.split() '-'.join('There can be only one.'.split())

['Remember,', 'remember,', 'the', 'fifth', 'of', 'November.'] 'There-can-be-only-one.'

What do the \n and \t escape characters represent?

\n is a newline; \t is a tab.

Write an assert statement that always triggers an AssertionError.

assert False, 'This assertion always triggers.'

Write an assert statement that triggers an AssertionError if the variables eggs and bacon contain strings that are the same as each other, even if their cases are different (that is, 'hello' and 'hello' are considered the same, and 'goodbye' and 'GOODbye' are also considered the same).

assert eggs.lower() != bacon.lower(), 'The eggs and bacon variables are the same!' or assert eggs.upper() != bacon.upper(), 'The eggs and bacon variables are the same!'

Write an assert statement that triggers an AssertionError if the variable spam is an integer less than 10.

assert spam >= 10, 'The spam variable is less than 10.'

How would you store all the attributes of a Beautiful Soup Tag object in a variable named linkElem?

linkElem.attrs

What line of code can you add to disable all logging messages in your program?

logging.disable(logging.CRITICAL)

How would you write a regex that matches a sentence where the first word is either Alice, Bob, or Carol; the second word is either eats, pets, or throws; the third word is apples, cats, or baseballs; and the sentence ends with a period? This regex should be case-insensitive. It must match the following: 'Alice eats apples.' 'Bob pets cats.' 'Carol throws baseballs.' 'Alice throws Apples.' 'BOB EATS CATS.' but not the following: 'Robocop eats apples.' 'ALICE THROWS FOOTBALLS.' 'Carol eats 7 cats.'

re.compile(r'(Alice|Bob|Carol)\s(eats|pets|throws)\s(apples|cats|baseballs)\.', re.IGNORECASE)

How would you write a regex that matches the full name of someone whose last name is Nakamoto? You can assume that the first name that comes before it will always be one word that begins with a capital letter. The regex must match the following: 'Satoshi Nakamoto' 'Alice Nakamoto' 'Robocop Nakamoto' but not the following: 'satoshi Nakamoto' (where the first name is not capitalized) 'Mr. Nakamoto' (where the preceding word has a nonletter character) 'Nakamoto' (which has no first name) 'Satoshi nakamoto' (where Nakamoto is not capitalized)

re.compile(r'[A-Z][a-z]*\sNakamoto')

How would you write a regex that matches a number with commas for every three digits? It must match the following: '42' '1,234' '6,368,745' but not the following: '12,34,567' (which has only two digits between the commas) '1234' (which lacks commas)

re.compile(r'^\d{1,3}(,\d{3})*$') will create this regex, but other regex strings can produce a similar regular expression.

Say you have a Beautiful Soup Tag object stored in the variable spam for the element <div>Hello world!</div>. How could you get a string 'Hello world!' from the Tag object?

spam.getText()


Kaugnay na mga set ng pag-aaral

Algebra 4.07: Transform Linear Functions

View Set

Fundamentos Históricos en Ciencias de la Salud Primer Parcial

View Set

Accounting for Decision Making Exam 1

View Set

Assessment and Management of Patients with Allergic Disorders

View Set

Sonography, Chapter 2, Ultrasound Instrumentation: "Kobology"

View Set

History Rock and Roll Final/Midterm use F3

View Set