QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3839|回复: 0
打印 上一主题 下一主题

Python-网页爬虫与Sqlite3

[复制链接]
字体大小: 正常 放大
檀俾九        

7

主题

3

听众

10

积分

升级  5.26%

该用户从未签到

自我介绍
我本名为我,那就是我
跳转到指定楼层
1#
发表于 2021-1-14 08:44 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
  1. #!/usr/bin/python
  2. * N\" B: p1 F2 b: e# U# -*- coding: utf-8 -*-
  3. # y\" o$ h' {+ G. ]* A8 {
  4. ' U% X5 n7 ]- b& `' iimport sqlite3
  5. $ L+ ^, Z$ ^6 vimport requests
  6. 0 ~- w- O2 c& T\" E0 Afrom bs4 import BeautifulSoup
  7. 1 j4 r; E6 D  gfrom re import escape
  8. \" U) \; {6 ^; [( e3 n  G6 t- p, @/ _4 U% u  g+ {+ C9 q' V1 ?
  9. if __name__ == '__main__':4 J5 y2 |$ e# }; x
  10.     conn = sqlite3.connect('Python.db')
  11. 7 B) X7 K0 c8 M0 c    c = conn.cursor()
  12. . O/ E2 R, h7 G8 x1 S% w# G    c.execute('''CREATE TABLE IF NOT EXISTS Python () E) Y0 |% i+ H\" D/ p# y8 ]* x
  13.         Url VARCHAR,
  14. 7 O2 ~9 ^& B4 U3 n9 F        Title VARCHAR,& w5 m! t\" g* J1 {
  15.         Author VARCHAR- {, t\" J8 [0 G5 S7 {/ [: h7 j
  16.     )''')
  17. , b, D) i( z( M\" v\" U- t/ ]; E4 K    conn.commit()  _& [% M5 ], P5 `\" [4 V
  18. 7 ?( O- b! f+ v3 w9 Q
  19.     # --------------------Split Line--------------------
  20. 3 @; \$ ~. S( N. s) G5 a    headers = {! S) X. A6 h4 j( ~' P, A) R% C1 y: _# w
  21.         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36"4 I4 [6 R, V) w9 a
  22.     }
  23. + ~$ T' B! P5 l5 W& U
  24. ' o6 `( C- o0 @- |9 X    for i in range(1, 1046):3 }7 {+ W; A2 y  V) {$ d. p& d# K( j/ k
  25.         url = "http://xxx/index_%s.html" % str(i)# ?; Z+ P  @+ _5 ?& y7 p
  26.         req = requests.get(url=url, headers=headers)
  27. : ^\" W5 `# W8 Y* W3 e        req.encoding = "utf-8"! N0 H( t7 m9 O( }
  28.         html = BeautifulSoup(req.text, "lxml")
  29. 6 c. l; l1 v; E  {; d\" U. y) B1 E) I' M  b/ H7 g
  30.         # --------------------Split Line--------------------' x1 F, c7 v& \6 `) Y& _: ^) [( W5 S
  31.         for div in html.find_all('div', class_='loop'):& d& z, {* K  ^8 Q' d) D% s
  32.             content_body = div.select('h2 > a')[0]
  33. 8 _4 v% w\" W7 n( A            content_infor = div.select('.content_infor > span:nth-child(3)')[0]
  34. $ T0 l7 @- p; y* V0 P. I/ s' ~7 W: \% d3 l- d
  35.             # --------------------Split Line--------------------
  36. 8 ?5 @$ \8 D- i9 n* C2 ~- S' g2 s            cursor = c.execute(
  37. $ F6 q' c+ S+ q5 i                "SELECT COUNT(*) FROM Python WHERE Url = '%s'" % ("http://xxx" + content_body.get('href')))1 A6 V) i9 C+ n$ V( f* _; N( H
  38.             len = 0\" D\" |( Z/ N  J! r' l9 n, d# \
  39.             for row in cursor:
  40. 7 m4 ]/ X( k& Y# I* O+ i1 u                len = row[0], t8 o+ U1 `# n. r  }) T
  41.             if len > 0:' H, h7 [- Q( K5 P: @9 P
  42.                 continue
  43. \" `; _0 T1 a; b& c  H$ o* w% Y* D) w' M& K2 R' n, [7 P: m
  44.             # --------------------Split Line--------------------: N- [9 V& |2 w0 D* h; I7 K
  45.             c.execute('INSERT INTO Python( Url, Title, Author) VALUES ( "%s", "%s", "%s")' % (4 j6 ?) f/ i& J\" L! h7 v! B
  46.                 "http://xxx" + content_body.get('href'),) [\" D\" M3 ~: a. _3 L4 A
  47.                 escape(content_body.get('title').replace("\"", "\"\"")),
  48. 3 _! z5 ~- l4 C. D. I' |4 I* q                content_infor.text.replace('xxx: ', '')))
  49. 7 p( L* d. C* {. u* E: o5 z6 s. X
  50. 2 i) _& U/ @! f1 |) x0 `        conn.commit()7 f9 q; x5 H6 a5 j
  51.         print("第%s页" % str(i))
  52. + N2 |+ W' H( |( d: p$ n- A+ Y0 U
  53. \" }2 z2 J9 e9 J6 [: [    # --------------------Split Line--------------------
  54.   {& n+ D& u6 r0 ?- U% x# x* r    conn.close()
  55. 7 e9 n- D& @1 G, S9 O  {$ u

" K" M" X: X" ?0 R1 M- h1 T* X/ n+ B( x) i- g4 [# I, q
转发自派生社区
Python交流群:1047602540
) c& @1 s* U0 ^
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-9-2 01:30 , Processed in 0.310199 second(s), 50 queries .

回顶部