用Python爬取文本信息

import requests
import re
import time

headers={
        'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.638.0 Safari/534.16'
        }
res=requests.get('http://www.kugou.com/yy/rank/home/%7B%7D-8888.html')
f=open('E:/doupo.txt','a+')
def get_info(url):
    res=requests.get(url,headers=headers)
    contexts=re.findall('<p>(.*?)</p>',res.content.decode('utf-8'),re.S)
    for context in contexts:
        f.write(context+'\n')
        
if __name__ == '__main__':
    urls=['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i))
    for i in range(2,24)]
    for url in urls:
        get_info(url)
    time.sleep(2)
print('{}'.format(res))
f.close()


import requests
import re
import time


headers={
        'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.638.0 Safari/534.16'
        }#模拟浏览器抓取数据
res=requests.get('http://www.kugou.com/yy/rank/home/%7B%7D-8888.html')
f=open('E:/doupo.txt','a+')#以追加的方式创建文档
def get_info(url):
    res=requests.get(url,headers=headers)
    contexts=re.findall('<p>(.*?)</p>',res.content.decode('utf-8'),re.S)#用正则表达式匹配要查找的内容
    for context in contexts:
        f.write(context+'\n')
        
if __name__ == '__main__':
    urls=['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i))
    for i in range(2,24)]
    for url in urls:
        get_info(url)
    time.sleep(2)
print('{}'.format(res))
f.close()
阅读更多
文章标签: Python 爬虫
个人分类: Python
想对作者说点什么? 我来说一句

爬虫源代码

2014年08月12日 5KB 下载

没有更多推荐了,返回首页

关闭
关闭