How to save search engine results in the Python Programming Language

Source: Internet
Author: User

The Python programming language is applied in many fields and also in the use of search engines, next we will introduce in detail the problems related to saving the search engine results in the Python programming language.

 
 
  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 = “content=\”text/html;charset=gb2312\”><title>kf701 python 
    search tool</title>
  43. html_header += “<p align=center><font size=3>kf701 python 
    search tool</font></p>”  
  44. html_end = “</body>
  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() 

The above is an introduction to the Code for saving search results in the Python programming language.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.