吴玉良简历,辣妹街头篮球,郭晓峰妻子
比如,你想采集标题中包含“58同城”的serp结果,并过滤包含有“北京”或“厦门”等结果数据。
该python脚本主要是实现以上功能。
其中,使用beautifulsoup来解析html,可以参考我的另外一篇文章:windows8下安装beautifulsoup
代码如下:
#获取html源码
def gethtml(url):
try:
req = urllib2.request(url)
response= urllib2.urlopen(req,none,3)#设置超时时间
data = response.read().decode('utf-8','ignore')
except:pass
return data
#提取搜索结果serp的标题
def fetchtitle(html):
try:
soup = beautifulsoup(''.join(html))
for i in soup.findall("h3"):
title = i.text.encode("utf-8")
if any(str_ in title for str_ in ("北京","厦门")):
continue
else:
print title
writefile("result.txt",title)
except:
pass
keyword = "58同城"
if __name__ == "__main__":
global keyword
start = time.time()
for i in range(0,8):
url = "http://www.baidu.com/s?wd=intitle:"+keyword+"&rn=100&pn="+str(i*100)
html = gethtml(url)
fetchtitle(html)
time.sleep(1)
c = time.time() - start
print('程序运行耗时:%0.2f 秒'%(c))
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论