Python实现抓取网页并且解析的实例
作者:shichen2014 时间:2022-01-12 13:24:53
本文以实例形式讲述了Python实现抓取网页并解析的功能。主要解析问答与百度的首页。分享给大家供大家参考之用。
主要功能代码如下:
#!/usr/bin/python
#coding=utf-8
import sys
import re
import urllib2
from urllib import urlencode
from urllib import quote
import time
maxline = 2000
wenda = 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程序设计的学习有所帮助。
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 字符串转列表 list 出现\\ufeff的解决方法
2023-11-24 18:47:06
你可能不知道的Python 技巧小结
2022-12-18 07:08:10
整理及优化CSS代码的七个原则[译]
2009-04-23 12:35:00
![](https://img.aspxhome.com/file/UploadPic/20094/23/01-21s.gif)
Python实现按中文排序的方法示例
2023-11-29 15:19:22
![](https://img.aspxhome.com/file/2023/2/92142_0s.png)
Tensorflow2.1 MNIST图像分类实现思路分析
2023-04-17 03:35:32
Python json 错误xx is not JSON serializable解决办法
2021-06-25 21:58:05
掀起抛弃IE6的高潮吧
2009-02-26 12:44:00
淘宝首页代码调整
2011-04-22 12:44:00
将滚动条(scrollbar)保持在最底部的方法
2008-02-21 10:05:00
MongoDB为用户设置访问权限
2023-07-16 06:03:52
tensorflow2.0如何实现cnn的图像识别
2022-05-01 21:19:58
![](https://img.aspxhome.com/file/2023/9/77809_0s.jpg)
python中dict()的高级用法实现
2022-06-16 23:24:29
![](https://img.aspxhome.com/file/2023/2/84122_0s.png)
Javascript 中 String.replace( ) 的妙用
2008-08-05 18:08:00
python人工智能tensorflow常见损失函数LOSS汇总
2023-11-22 18:12:48
![](https://img.aspxhome.com/file/2023/7/64277_0s.png)
JSP request.setAttribute()详解及实例
2023-06-18 23:59:32
python 将字符串转换成字典dict的各种方式总结
2022-06-28 21:13:35
JavaScript调试之console.log调试的一个小技巧分享
2023-06-28 17:23:49
![](https://img.aspxhome.com/file/2023/3/55903_0s.png)
如何从SQL数据库中调用图片?
2009-11-15 19:59:00
关于鼠标、键盘的几个事件的例子
2008-07-27 17:00:00
为FCKeditor2.6添加行距功能(最新修改)
2008-08-18 21:09:00