|
|
众所周知,Python作为编程语言的一种,不仅语法简单、易学并且很有前途,很多人都对Python感兴趣。Python的应用十分广泛。经常关注我的朋友可能知道,关于Python的使用,之前已经为大家介绍了很多用法,今天主要介绍两个方面的内容,一个是如何使用Python构建打字机器人,另一是如何使用Python进行Web抓取的内容。& L* [- v" k0 o" o& E+ K* u4 {1 [
一、如何使用Python构建打字机器人
* }+ K# c6 w( E+ a2 D" K' R, \$ m# d 先决条件% C3 S6 o& o1 C( F7 R/ E
) I# @ H( `( U: N! i1 ?
代码段3 @+ P* B7 y) H t) j: G
- <p> # Importing required modules and initializing variablesfrom selenium import ) n! @& h. S: N& M" x
- webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport
6 N: N: n' N3 W - re</p>
: P+ e6 \! E: C+ ~+ h - <p> string = ''</p>4 Q: _: ^7 e3 l2 t% @9 ~( s
- <p> # ______________________________________</p>
5 y$ |6 Q9 v, Q3 }# ?' L - <p> # Opening thetypingcat.com on firefox</p>
. I. U8 z4 e+ O. S- I - <p> firefox = webdriver.Firefox()</p>4 Z5 i Y# ~2 |. E9 I. D
- <p> firefox.get</p>! Y3 Y( r+ x/ |0 i I
- <p> # ______________________________________</p>
0 T1 [9 _: C0 S+ `5 c! z' Z - <p> # Using javascript to get the typing content from the website and storing
: Q+ L3 t9 r& C* E6 l. f9 |; T6 v - value in "string" variablefor i in range(firefox.execute_script('return ; B- ?; \' s) u
- document.querySelectorAll(".line").length')):</p>* R9 o2 @1 Q+ U# a6 p8 K% r
- <p> string += firefox.execute_script('return
8 D4 R3 K4 X: s' S - document.querySelectorAll(".line")['+str(i)+'].innerHTML')</p>
8 [6 k9 u3 S, V - <p> string = re.sub(r'<[^>]*>','',string) #This line is just delete
9 v5 k# G) r) ~6 b& P - tags present inside string</p>
0 H" N/ B, a2 w6 D - <p> # ______________________________________</p>. D9 @, ~5 o( z, Z4 ~7 g
- <p> # Selenium commands to type what is stored inside string variable on the
4 @$ p1 u( j/ V% N - focused screen</p>3 u1 W/ ^5 V7 u) l8 X8 M3 ]0 P% y; [, b
- <p> action = ActionChains(firefox)</p>
& B& i9 V: D$ y' j' L - <p> action.send_keys(string)</p>
/ S8 x, M& c! e/ ] - <p> action.perform()</p>
6 |' P' {. O# a8 a& U5 O - <p> # ______________________________________ END ; Q& q9 n& D. q7 s
- ______________________________________</p>
复制代码 / O* w3 E# ?: ?) _% n+ v4 y _8 i; S% ^
二、如何使用Python进行Web抓取! a1 F$ ~; ]4 x& o9 q3 ~# Z
仅需9行代码即可使用Python进行Web抓取
/ A u: ] x3 G0 t* Z8 a% @$ D/ T4 b 先决条件6 G! ?3 b" o! s/ l4 k' h0 m- A
如果还没有安装,请安装beautifulsoup4和请求。
+ K7 b; s# n: o2 h- <p> pip install beautifulsoup4</p>
O% J4 n' ^3 \( T2 R; _" G# z - <p> pip install requests</p>
复制代码 + Y9 U2 z2 B k2 D2 i k
代码段
" y# H* q9 R/ w( \- <p> import requestsfrom bs4 import BeautifulSoup</p>: R/ \0 U8 M% m
- <p> result = requests.get</p>
2 d& |/ E! _. X- h7 C2 k4 } - <p> soup = BeautifulSoup(result.content)</p>2 y& M# v0 g5 ]+ [6 s0 Q, C
- <p> links = soup.find_all("a", "title")</p>
/ Q3 r J o1 u, v8 n - <p> data = {}for link in links:</p># n) F: f5 \& B! z5 _
- <p> title = link.string</p>0 M2 Y% b* |0 y, H& Z3 ]0 w
- <p> data[title] = link.attrs['href']</p>
复制代码 2 Z1 s3 V3 `; g* K+ y' U) I8 P9 A; m
这里是完整的代码段,您可以将其直接复制并粘贴到终端,收藏的文本编辑器或jupyter笔记本中。
( N$ [' Q, V: M' ^ 要检查您是否正确执行了该操作,数据输出应类似于以下内容:5 j: } M- L0 q d
- <p> {'MSI GL62VR 7RFX': '/test-sites/e-commerce/allinone/product/326',</p> Y" |) l0 ~2 ~* `* h v1 r( U$ r0 z" p
- <p> 'Dell Vostro 15…': '/test-sites/e-commerce/allinone/product/283',</p>
1 Z1 m9 v( a# ]: ~ - <p> 'Dell Inspiron 17…': '/test-sites/e-commerce/allinone/product/296'}</p>
复制代码 - \6 n) p6 n' K9 K
Web抓取功能很棒,当您想从网站中快速提取数据时,可以节省大量时间。上面的示例可帮助您快速入门。5 J3 C8 J2 d+ {. O6 f
好了以上就是关于如何使用Python构建打字机器人,以及进行Web抓取的全部内容介绍。
+ [8 [. E% s1 o% W U( |2 u. x" v, H9 Q3 t' C
|
|