您的位置:首页 > 理论基础 > 计算机网络

Python学习之Http

2012-06-10 21:39 232 查看
留下学习资料、方便自己和别人使用。

主要使用http.client 和 re 模块 进行网页数据的抓取和整理

import http.client
import re

def RandomSong():
httpCon = http.client.HTTPConnection("music.sogou.com")
httpCon.request("GET", "/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&&id=1&jsoncallback=jsonp1339165645104")
response = httpCon.getresponse()
data=response.read().decode('GBK')
matchs= re.findall("(?=\"title\":\")[^\}]+", data)
if matchs:
for m in matchs:
title=re.search("(?<=\"title\":\")[^\"]+", m)
singer=re.search("(?<=\"singer\":\")[^\"]+", m)
if title:
str='title:'+title.group()+' singer:'+singer.group()
print(str)
else:
print('not match')

RandomSong()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: