python爬虫,爬取虎扑网新闻
2017-09-16 22:48
274 查看
以前写过的代码过一段时间就会忘记,需要按时复习
最近闲来无事,写了一个简单的爬虫程序,无奈知识遗忘太快,竟然花了我好长时间
女票喜欢库里,但无奈库里新闻太少,只好爬一波勇士队消息
【女票是不会喜欢从文件中看信息的。。。但我还是要写】
最近闲来无事,写了一个简单的爬虫程序,无奈知识遗忘太快,竟然花了我好长时间
女票喜欢库里,但无奈库里新闻太少,只好爬一波勇士队消息
【女票是不会喜欢从文件中看信息的。。。但我还是要写】
# -*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup import re import xlrd def get_content(hupu_url): headers={'User-agent':'Mozilla/5.0'} try: page=requests.get(hupu_url,headers=headers,timeout=3) except: return 'False','' content=page.text soup=BeautifulSoup(content,'lxml') title=soup.title #print(title.contents[0]) bodys=soup.find_all(class_="artical-main-content") #print(bodys) body=BeautifulSoup(str(bodys[0]),'lxml') tips=body.find_all("p") main_content="" for tip in tips: if "img" not in str(tip) and "href" not in str(tip): tip=re.findall(r'<p>(.*)?</p>',str(tip)) main_content+='\t'+str(tip[0])+'\n' #print(main_content) return title.contents[0],main_content def writetofile(title,content): with open('ToWenWen.txt','a',encoding='utf-8') as f: news='<title>'+'\n'+'\t'+str(title)+'\n'+'<content>'+'\n'+str(content) f.write(news) for i in range(0,100): f.write('==') f.write('\n') if __name__=='__main__': f=open("ToWenWen.txt",'wt') f.close() for j in range(1,15): warriors_url='https://voice.hupu.com/nba/tag/2982-'+str(j)+'.html' headers={'User-agent':'Mozilla/5.0'} try: page=requests.get(warriors_url,headers=headers,timeout=15) except: continue; content=page.text soup=BeautifulSoup(content,'lxml') hupu=soup.find_all(class_="list-content") hupu2=BeautifulSoup(str(hupu),'lxml') hupu3=hupu2.find_all(class_="n1") news_urls=re.findall('<a href="(.*)?" target="_blank">',str(hupu3)) #print(news_urls) for hupu_url in news_urls: print('search url',hupu_url) try: title,content=get_content(hupu_url) writetofile(title,content) except: continue
相关文章推荐
- Python爬虫爬取新浪新闻内容
- Python写爬虫-爬甘农大学校新闻
- Python写爬虫-爬甘农大学校新闻
- python爬虫抓取新华网新闻并自动生成word文档
- 【Python】爬虫爬取各大网站新闻(一)
- Python爬虫:新浪新闻详情页的数据抓取(函数版)
- Python爬虫:获取新浪网新闻
- python爬虫爬取合肥工业大学校园新闻
- python3爬虫 爬取图片,爬取新闻网站文章并保存到数据库
- python简单爬虫,Beautifulsoup4解析,爬取直播吧部分热门新闻及链接
- 19Python爬虫--爬取新浪新闻标题并保存到数据库
- python爬虫爬取Bloomberg新闻
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
- python爬虫 根据关键字在新浪网站查询跟关键字有关的新闻条数(按照时间查询)
- Python爬虫项目,获取所有网站上的新闻,并保存到数据库中,解析html网页等(未完待续)
- python虎扑社区论坛数据爬虫分析报告
- python3.5 beautiful4.4 扣扣国内新闻 爬虫
- python实现虎扑网站图片爬虫
- Python爬虫抓取虎扑论坛帖子图片
- python3+urllib撸新浪滚动新闻爬虫