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

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

[复制链接]

尚未签到

发表于 2020-8-27 17:56:03 | 显示全部楼层 |阅读模式
  众所周知,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
  1.   pip install selenium
复制代码
, u0 _: n& L" l
  代码段6 Y% K0 `- Y2 l
  1. <p>  # Importing required modules and initializing variablesfrom selenium import
    ( P5 w% O, s! D, D% y) B8 o
  2. webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport 2 Y' {2 l, S7 U% h
  3. re</p>- Y. ?( {! q$ S3 K% T4 }
  4. <p>  string = ''</p>9 A, _1 p7 M6 d& l
  5. <p>  # ______________________________________</p>% O* x" T' _5 T7 Z; Y. Z
  6. <p>  # Opening thetypingcat.com on firefox</p>
    ) M0 O* f5 T( Z2 _( B. y
  7. <p>  firefox = webdriver.Firefox()</p>
    ; ]: D9 p! A# i- s- k
  8. <p>  firefox.get</p>$ z$ n! ~* w. ]0 Z6 Q4 r
  9. <p>  # ______________________________________</p>
    4 `# s% K' f5 s# d7 H, l( P+ \
  10. <p>  # Using javascript to get the typing content from the website and storing 7 }# s4 Q2 U3 p9 K" b
  11. value in "string" variablefor i in range(firefox.execute_script('return
      j" X: H3 w8 N# `; k
  12. document.querySelectorAll(".line").length')):</p>& |+ ~, |) u; W
  13. <p>  string += firefox.execute_script('return 2 t$ S) h, [; g  M+ o# o
  14. document.querySelectorAll(".line")['+str(i)+'].innerHTML')</p>
    ) N8 I& }( n  L# t/ R$ S
  15. <p>  string = re.sub(r'<[^>]*>','',string) #This line is just delete
    7 X1 L0 {1 L7 T" n1 L% A
  16. tags present inside string</p>
    + E* _) W: R" ?
  17. <p>  # ______________________________________</p>
    ' E0 q9 w" O; M  [
  18. <p>  # Selenium commands to type what is stored inside string variable on the
    5 \( K* {' I+ P- f7 J
  19. focused screen</p>
    # q# y6 c8 S5 Q) ]- `! f  V
  20. <p>  action = ActionChains(firefox)</p>5 Y( }, O& ~, l) W: ?6 i
  21. <p>  action.send_keys(string)</p>  ]4 D; D% X" [8 z. _
  22. <p>  action.perform()</p>0 T9 @0 R+ c0 S  g' D2 I
  23. <p>  # ______________________________________ END : _3 U7 {! |  p/ d
  24. ______________________________________</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
  1. <p>  pip install beautifulsoup4</p>7 ~2 ?! y# Z$ E
  2. <p>  pip install requests</p>
复制代码

' A. f' @1 A$ o/ U" }  代码段
: @0 L- s4 i% J
  1. <p>  import requestsfrom bs4 import BeautifulSoup</p>( }1 S% a. b$ s6 v6 M) [$ S
  2. <p>  result = requests.get</p>: y' y2 {& V! C+ W% x
  3. <p>  soup = BeautifulSoup(result.content)</p>+ E6 c; E/ J7 g3 J& u
  4. <p>  links = soup.find_all("a", "title")</p>( E6 H$ B! _% T, y2 [
  5. <p>  data = {}for link in links:</p>+ r8 e7 \; F$ V
  6. <p>  title = link.string</p>$ q1 c9 v- m  n. X& _3 f
  7. <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
  1. <p>  {'MSI GL62VR 7RFX': '/test-sites/e-commerce/allinone/product/326',</p>
    8 y/ \  d( v* @  P
  2. <p>  'Dell Vostro 15…': '/test-sites/e-commerce/allinone/product/283',</p>' b5 C, N: i- `) V4 }# A
  3. <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$ \
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

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

点击这里给我发消息

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

GMT+8, 2026-10-28 07:37

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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