自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python編程語言如何保存搜索引擎結(jié)果

開發(fā) 后端
搜索引擎的結(jié)果有些時候需要我們進行保存,這是可以用Python編程語言來達到的。下面我們就來看看相關(guān)的代碼是如何編寫的。

Python編程語言在很多的領(lǐng)域都會有應(yīng)用,在搜索引擎的使用中也會有應(yīng)用,下面我們就詳細的向大家介紹有關(guān)Python編程語言在保存搜索引擎結(jié)果的相關(guān)問題,希望大家喜歡。

  1. #!/usr/bin/env python  
  2. #-*- encoding:utf-8 -*-  
  3. import sys  
  4. import re  
  5. import httplib  
  6. def request_and_save(conn, query_str, f):  
  7. conn.request(”GET”, query_str)  
  8. r1 = conn.getresponse()  
  9. line = r1.read()  
  10. p1 = re.compile(”<a href=”)  
  11. p2 = re.compile(”</a>”)  
  12. match_start = 0 
  13. match_end = 0 
  14. while line:  
  15. m = p1.search(line)  
  16. if m is None:  
  17. break  
  18. mmatch_start = m.start()  
  19. lineline = line[match_start:]  
  20. m = p2.search(line)  
  21. if m is None:  
  22. break  
  23. mmatch_end = m.end()  
  24. item = line[:match_end]  
  25. if item.find(”class=l”) == -1:  
  26. lineline = line[match_end:]  
  27. continue  
  28. f.write(item)  
  29. f.write(”<br>\n”);  
  30. lineline = line[match_end:]  
  31. # end function  
  32. if len(sys.argv) < 2: 
  33. print “Usage: google.py words”  
  34. sys.exit(0)  
  35. query_str = “/search?aq=f\&complete=1\&hl=zh-CN\&newwind
    ow
    =1\&q=” + sys.argv[1]  
  36. query_str2 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=10\&sa=N”  
  37. query_str3 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=20\&sa=N”  
  38. query_str4 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=30\&sa=N”  
  39. query_str5 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=40\&sa=N”  
  40. query_str6 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=50\&sa=N”  
  41. query_str7 = “/search?q=” + sys.argv[1] + “\&complete=1\
    &
    hl=zh-CN\&newwindow=1\&start=60\&sa=N”  
  42. html_header = “<html><head><meta http-equiv=\”content-type\” 
    content=\”text/html;charset=gb2312\”><title>kf701 python 
    search tool
    </title></head><body>\n”  
  43. html_header += “<p align=center><font size=3>kf701 python 
    search tool
    </font></p>”  
  44. html_end = “</body></html>”  
  45. conn = httplib.HTTPConnection(”www.google.cn“)  
  46. print ‘Search ‘ + sys.argv[1] + ‘, Save result in ‘ + sys.
    argv[1] + ‘-search.html’  
  47. f = file( sys.argv[1] + “-search.html”, “w”)  
  48. f.write(html_header);  
  49. request_and_save(conn, query_str, f)  
  50. request_and_save(conn, query_str2, f)  
  51. request_and_save(conn, query_str3, f)  
  52. request_and_save(conn, query_str4, f)  
  53. request_and_save(conn, query_str5, f)  
  54. request_and_save(conn, query_str6, f)  
  55. request_and_save(conn, query_str7, f)  
  56. f.write(html_end)  
  57. f.close()  
  58. conn.close() 

 

以上就是對Python編程語言在保存搜索結(jié)果的相關(guān)代碼介紹。

【編輯推薦】

  1. Python字符串操作方法的五種具體方法
  2. Python線程編程的主要表達方式詳解
  3. Python編程語言與Ruby之間的比較
  4. Python django應(yīng)用中的五大關(guān)鍵步驟
  5. Python主線程如何進行相關(guān)應(yīng)用
責(zé)任編輯:張浩 來源: IT168
相關(guān)推薦

2019-07-10 13:17:07

大數(shù)據(jù)搜索代碼

2010-03-10 09:28:41

Python標(biāo)準(zhǔn)庫

2011-06-20 18:23:06

SEO

2009-07-30 10:40:56

搜索引擎優(yōu)化網(wǎng)站

2023-02-08 10:45:23

2011-07-21 16:32:07

SEO

2017-08-07 08:15:31

搜索引擎倒排

2020-03-20 10:14:49

搜索引擎倒排索引

2022-03-31 20:15:21

圖像搜索引擎

2015-08-31 10:41:58

搜索引擎Google云應(yīng)用

2022-10-08 09:13:18

搜索引擎?站

2012-09-07 13:22:21

搜索搜狗

2016-08-18 00:54:59

Python圖片處理搜索引擎

2009-02-19 09:41:36

搜索引擎搜狐百度

2010-04-20 11:43:46

2009-09-22 16:23:52

搜索引擎

2017-08-21 11:14:36

2010-06-13 16:27:28

搜索引擎

2023-01-03 15:42:29

機器學(xué)習(xí)視頻搜索

2022-02-24 00:07:22

Shodan網(wǎng)絡(luò)搜索
點贊
收藏

51CTO技術(shù)棧公眾號