找回密码
 立即注册
查看: 763|回复: 0

如何使用Python构建打字机器人,以及进行Web抓取

[复制链接]

尚未签到

发表于 2020-8-27 17:56:03 | 显示全部楼层 |阅读模式
  众所周知,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
  1.   pip install selenium
复制代码
) I# @  H( `( U: N! i1 ?
  代码段3 @+ P* B7 y) H  t) j: G
  1. <p>  # Importing required modules and initializing variablesfrom selenium import ) n! @& h. S: N& M" x
  2. webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport
    6 N: N: n' N3 W
  3. re</p>
    : P+ e6 \! E: C+ ~+ h
  4. <p>  string = ''</p>4 Q: _: ^7 e3 l2 t% @9 ~( s
  5. <p>  # ______________________________________</p>
    5 y$ |6 Q9 v, Q3 }# ?' L
  6. <p>  # Opening thetypingcat.com on firefox</p>
    . I. U8 z4 e+ O. S- I
  7. <p>  firefox = webdriver.Firefox()</p>4 Z5 i  Y# ~2 |. E9 I. D
  8. <p>  firefox.get</p>! Y3 Y( r+ x/ |0 i  I
  9. <p>  # ______________________________________</p>
    0 T1 [9 _: C0 S+ `5 c! z' Z
  10. <p>  # Using javascript to get the typing content from the website and storing
    : Q+ L3 t9 r& C* E6 l. f9 |; T6 v
  11. value in "string" variablefor i in range(firefox.execute_script('return ; B- ?; \' s) u
  12. document.querySelectorAll(".line").length')):</p>* R9 o2 @1 Q+ U# a6 p8 K% r
  13. <p>  string += firefox.execute_script('return
    8 D4 R3 K4 X: s' S
  14. document.querySelectorAll(".line")['+str(i)+'].innerHTML')</p>
    8 [6 k9 u3 S, V
  15. <p>  string = re.sub(r'<[^>]*>','',string) #This line is just delete
    9 v5 k# G) r) ~6 b& P
  16. tags present inside string</p>
    0 H" N/ B, a2 w6 D
  17. <p>  # ______________________________________</p>. D9 @, ~5 o( z, Z4 ~7 g
  18. <p>  # Selenium commands to type what is stored inside string variable on the
    4 @$ p1 u( j/ V% N
  19. focused screen</p>3 u1 W/ ^5 V7 u) l8 X8 M3 ]0 P% y; [, b
  20. <p>  action = ActionChains(firefox)</p>
    & B& i9 V: D$ y' j' L
  21. <p>  action.send_keys(string)</p>
    / S8 x, M& c! e/ ]
  22. <p>  action.perform()</p>
    6 |' P' {. O# a8 a& U5 O
  23. <p>  # ______________________________________ END ; Q& q9 n& D. q7 s
  24. ______________________________________</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
  1. <p>  pip install beautifulsoup4</p>
      O% J4 n' ^3 \( T2 R; _" G# z
  2. <p>  pip install requests</p>
复制代码
+ Y9 U2 z2 B  k2 D2 i  k
  代码段
" y# H* q9 R/ w( \
  1. <p>  import requestsfrom bs4 import BeautifulSoup</p>: R/ \0 U8 M% m
  2. <p>  result = requests.get</p>
    2 d& |/ E! _. X- h7 C2 k4 }
  3. <p>  soup = BeautifulSoup(result.content)</p>2 y& M# v0 g5 ]+ [6 s0 Q, C
  4. <p>  links = soup.find_all("a", "title")</p>
    / Q3 r  J  o1 u, v8 n
  5. <p>  data = {}for link in links:</p># n) F: f5 \& B! z5 _
  6. <p>  title = link.string</p>0 M2 Y% b* |0 y, H& Z3 ]0 w
  7. <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
  1. <p>  {'MSI GL62VR 7RFX': '/test-sites/e-commerce/allinone/product/326',</p>  Y" |) l0 ~2 ~* `* h  v1 r( U$ r0 z" p
  2. <p>  'Dell Vostro 15…': '/test-sites/e-commerce/allinone/product/283',</p>
    1 Z1 m9 v( a# ]: ~
  3. <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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

点击这里给我发消息

QQ|小黑屋|Archiver|手机版|一路学IT论坛 专业视频教程网站

GMT+8, 2026-10-28 20:52

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表