The basic use of selenium:
from selenium Import the webdriver
# Import keyboard Keys
from selenium.webdriver.common.keys Import Keys
Import Time
Driver = webdriver.Chrome ()
# detector block
the try :
# Implicit wait, wait label loading
driver.implicitly_wait (10 )
# to Home jingdong transmission request
driver.get ( ' https://www.jd.com/ ' )
# Find input by the input box ID
The input_tag = driver.find_element_by_id ( ' Key ' )
# send_keys pass the current tag value
input_tag.send_keys ( ' Chinese Dictionary ' )
# press the keyboard's Enter key
input_tag.send_keys (Keys.ENTER)
the time.sleep ( 3 )
'' '
crawling Jingdong commodity information:
doll
name
url
price
evaluation
' ''
# Element to find a
# Elements find more
# find all the products list
good_list = driver.find_elements_by_class_name ( ' GL-Item ' )
# Print (good_list)
# loop through each item
for Good in good_list:
#Find by attribute selector product details page URL
# URL
good_url = good.find_element_by_css_selector ( ' .p IMG-A ' ) .get_attribute ( ' the href ' )
Print (good_url)
# Name
good_name = good.find_element_by_css_selector ( ' EM-name .p ' ) .text
Print (good_name)
# price
good_price = good.find_element_by_class_name ( ' P-. price ' ) .text
Print (good_price)
# evaluation number
good_commit = good.find_element_by_class_name (' P-the commit ' ) .text
Print (good_commit)
str1 = F '' '
URL: {} good_url
Name: {good_name}
Price: {good_price}
Evaluation: good_commit} {
\ n-
' ''
# the commodity information written text in
with Open ( ' jd.txt ' , ' A ' , encoding = ' UTF-. 8 ' ) AS F:
f.write (str1)
the time.sleep ( 10 )
# catch exception
the except exception AS E:
Print (E)
# Will ultimately drive the browser closes off
a finally :
driver.close ()
Data type of use:
int, float, str, BOOL, List, dict, Complex, tuple, the SET
*****: Key
Key methods of data types!
Variable immutable type!
Data type conversion!
Non-key:
Data type common method
isinstance (obj, type): based, stored on a 1/0 True / False nature.
String normal operation: ******
1, the index value string: a plurality of index values can be values are ordered. Example: str = "123abc"
Use: string name + [index]
2, string concatenation: with a "+" or a placeholder "% s".
3, the length of the string: len (string)
4, the string sections: Take substring. Variable name = string [start: end: step]
S STR = [0:. 3:. 1] STR S = [-1: -3: -1]
. 5, a string loop (traversing): for i in the string :
. 6, operation members C: determining whether a character string in the string:
BOOL string value for a string in the result
string important ways: *****
1, index: the index position of the target string:
string = .index position ( "target character string")
2, to clear space string: Strip
string .strip ()
3, calculate a target string Occurrence: COUNT
. 4, determines whether a string is digital: isdigit ()
String .isdigit (): bool result value is determined only positive integer.
5, case conversion: upper / lower string .upper / lower
capitalized: capitalize ()
of each word capitalized: title ()
. 6, determines whether a certain beginning or end: startwith () / endwith () , bool result value
7, substitutions: replace (Old, new new, COUNT)
. 8, format: format
list types: internal list can store a plurality of values, may be any type of
list may be nested
common operations list:
1, index value:
2, list of operators: +, *. Get a new list.
3, list length: len (list)
4, a list of slice: [::]
5, members of operation: target member in the target list
6, loop: for i in the target list:
a list of common methods:
1, add or delete a list of change search:
by: list .append (): by the end of the
list .insert (index, value): index by arbitrary position before
change: list [index] = value of the target changed
deleted: list .remove (target value)
List .pop (): defaults delete from the end, and return. You can delete (index) specified position
del list [index]
list .clear (): Empty list
2. Other methods:
Sort: Sort according to objective rules: List .sort (): Default forward ordering
for the same type!
Reverse the sort: List .sort (reverse = True)
list .reverse (): name is not called reverse ordering
number Calcd: List .count (): the list can store duplicate data
for calculating a target value of the stored frequency and .
The overall increase, added to the end: List .extend ( "iterable")
index position of the target range can be specified:
List .index (target audience, [starting position, [End position]])
*** variable and can not be *** variable type:
variable type: value change, address change.
Immutable type: value is changed, the address change.
Learn Content:
1. Find | rfind: Find substring index, no results are returned -1
4. center | ljust | rjust | zfill : bitwise filled
2. lstrip: Go left blank
3. rstrip: Right to blank
Syntax: center (median percentage, 'fill symbol')
5. expandtabs: provisions \ t the number of occupied spaces
6. captialize | title | swapcase: capitalized | capitalized words | reverse case
7. isdigit | isdecimal | isnumeric: Analyzing digital
8. isalnum | isalpha: alphanumeric whether | the alphabet
9. isidentifier: is a valid identifier
10. islower | isupper: whether the full fine | caps
11 . isspace: whether whitespace
12. istitle: whether the capitalized word format