|
|
众所周知,Python作为编程语言的一种,不仅语法简单、易学并且很有前途,很多人都对Python感兴趣。Python的应用十分广泛。经常关注我的朋友可能知道,关于Python的使用,之前已经为大家介绍了很多用法,今天主要介绍两个方面的内容,一个是如何使用Python构建打字机器人,另一是如何使用Python进行Web抓取的内容。! b" O8 P6 r7 |) |. O( W0 l
一、如何使用Python构建打字机器人/ M$ ?* |9 U7 [- V2 O, Q
先决条件
6 n; U0 e, k* R$ l, T, u0 _: n& L" l
代码段6 Y% K0 `- Y2 l
- <p> # Importing required modules and initializing variablesfrom selenium import
( P5 w% O, s! D, D% y) B8 o - webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport 2 Y' {2 l, S7 U% h
- re</p>- Y. ?( {! q$ S3 K% T4 }
- <p> string = ''</p>9 A, _1 p7 M6 d& l
- <p> # ______________________________________</p>% O* x" T' _5 T7 Z; Y. Z
- <p> # Opening thetypingcat.com on firefox</p>
) M0 O* f5 T( Z2 _( B. y - <p> firefox = webdriver.Firefox()</p>
; ]: D9 p! A# i- s- k - <p> firefox.get</p>$ z$ n! ~* w. ]0 Z6 Q4 r
- <p> # ______________________________________</p>
4 `# s% K' f5 s# d7 H, l( P+ \ - <p> # Using javascript to get the typing content from the website and storing 7 }# s4 Q2 U3 p9 K" b
- value in "string" variablefor i in range(firefox.execute_script('return
j" X: H3 w8 N# `; k - document.querySelectorAll(".line").length')):</p>& |+ ~, |) u; W
- <p> string += firefox.execute_script('return 2 t$ S) h, [; g M+ o# o
- document.querySelectorAll(".line")['+str(i)+'].innerHTML')</p>
) N8 I& }( n L# t/ R$ S - <p> string = re.sub(r'<[^>]*>','',string) #This line is just delete
7 X1 L0 {1 L7 T" n1 L% A - tags present inside string</p>
+ E* _) W: R" ? - <p> # ______________________________________</p>
' E0 q9 w" O; M [ - <p> # Selenium commands to type what is stored inside string variable on the
5 \( K* {' I+ P- f7 J - focused screen</p>
# q# y6 c8 S5 Q) ]- `! f V - <p> action = ActionChains(firefox)</p>5 Y( }, O& ~, l) W: ?6 i
- <p> action.send_keys(string)</p> ]4 D; D% X" [8 z. _
- <p> action.perform()</p>0 T9 @0 R+ c0 S g' D2 I
- <p> # ______________________________________ END : _3 U7 {! | p/ d
- ______________________________________</p>
复制代码
- {: X; J9 j( n) q2 [" k; L 二、如何使用Python进行Web抓取
R- s! v. D6 X0 o' v. h! U. W 仅需9行代码即可使用Python进行Web抓取: r" n" X8 f0 q2 z8 B- H# n2 _
先决条件
; _9 T& I5 ~8 }' E4 B 如果还没有安装,请安装beautifulsoup4和请求。
& K* d% L+ V; J" G8 P( C- <p> pip install beautifulsoup4</p>7 ~2 ?! y# Z$ E
- <p> pip install requests</p>
复制代码
' A. f' @1 A$ o/ U" } 代码段
: @0 L- s4 i% J- <p> import requestsfrom bs4 import BeautifulSoup</p>( }1 S% a. b$ s6 v6 M) [$ S
- <p> result = requests.get</p>: y' y2 {& V! C+ W% x
- <p> soup = BeautifulSoup(result.content)</p>+ E6 c; E/ J7 g3 J& u
- <p> links = soup.find_all("a", "title")</p>( E6 H$ B! _% T, y2 [
- <p> data = {}for link in links:</p>+ r8 e7 \; F$ V
- <p> title = link.string</p>$ q1 c9 v- m n. X& _3 f
- <p> data[title] = link.attrs['href']</p>
复制代码 ( y0 x! S% d( C
这里是完整的代码段,您可以将其直接复制并粘贴到终端,收藏的文本编辑器或jupyter笔记本中。
, G0 c4 \1 U5 _, |1 F3 F 要检查您是否正确执行了该操作,数据输出应类似于以下内容:8 [) m$ l& A" ~- U) `2 [% O
- <p> {'MSI GL62VR 7RFX': '/test-sites/e-commerce/allinone/product/326',</p>
8 y/ \ d( v* @ P - <p> 'Dell Vostro 15…': '/test-sites/e-commerce/allinone/product/283',</p>' b5 C, N: i- `) V4 }# A
- <p> 'Dell Inspiron 17…': '/test-sites/e-commerce/allinone/product/296'}</p>
复制代码 ; _6 Z8 q% |5 j4 }. `
Web抓取功能很棒,当您想从网站中快速提取数据时,可以节省大量时间。上面的示例可帮助您快速入门。
! b1 Y* m# J& F 好了以上就是关于如何使用Python构建打字机器人,以及进行Web抓取的全部内容介绍。& P5 E+ U _! [( J- }7 S1 |
9 V( O& C# m E$ \ |
|