国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現抓取網頁并且解析的實例

2020-02-23 05:49:11
字體:
來源:轉載
供稿:網友

本文以實例形式講述了Python實現抓取網頁并解析的功能。主要解析問答與百度的首頁。分享給大家供大家參考之用。

主要功能代碼如下:

#!/usr/bin/python#coding=utf-8import sys import reimport urllib2from urllib import urlencodefrom urllib import quoteimport timemaxline = 2000wenda = re.compile("href=/"http://wenda.so.com/q/.+/?src=(.+?)/"")baidu = re.compile("<a href=/"http://www.baidu.com/link/?url=.+/".*?>更多知道相關問題.*?</a>")f1 = open("baidupage.txt","w")f2 = open("wendapage.txt","w")for line in sys.stdin:  if maxline == 0:    break  query = line.strip();  time.sleep(1);  recall_url = "http://www.so.com/s?&q=" + query;  response = urllib2.urlopen(recall_url);  html = response.read();                                                     f1.write(html)  m = wenda.search(html);  if m:    if m.group(1) == "110":      print query + "/twenda/t0";    else:      print query + "/twenda/t1";  else:    print query + "/twenda/t0";  recall_url = "http://www.baidu.com/s?wd=" + query +"&ie=utf-8";  response = urllib2.urlopen(recall_url);  html = response.read();  f2.write(html)  m = baidu.search(html);  if m:    print query + "/tbaidu/t1";  else:    print query + "/tbaidu/t0";  maxline = maxline - 1;f1.close()f2.close()

希望本文所述對大家Python程序設計的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岚皋县| 宁陕县| 和龙市| 邵阳市| 富阳市| 芦溪县| 文化| 万盛区| 安徽省| 河源市| 大理市| 万山特区| 冀州市| 贡嘎县| 青田县| 偏关县| 会宁县| 辰溪县| 永和县| 庆云县| 武邑县| 汤原县| 从化市| 景谷| 闻喜县| 永泰县| 荣成市| 光山县| 安远县| 乐陵市| 长岭县| 沙湾县| 鹿泉市| 南昌市| 甘泉县| 柳江县| 项城市| 大关县| 荃湾区| 宁夏| 嘉禾县|