qq:800819103
在线客服,实时响应qq群
在线客服,实时响应客服电话
13318873961网上每天都有大量的新闻内容出现,如何快速获取这些内容呢?今天黑洞代理IP就为大家分享一下,爬取是如何爬取网易的社会新闻内容的。首先我们可以先打开目标页面,右键检查,查找我们需要的内容。这里我们主要抓取新闻标题、链接和新闻内容。
代码如下:
import re
from urllib import request
from bs4 import BeautifulSoup
def download(title, url):
req = request.urlopen(url)
res = req.read()
soup = BeautifulSoup(res,'lxml')
#print(soup.prettify())
tag = soup.find('div',class_='post_text')
#print(tag.get_text())
title = title.replace(':','')
title = title.replace('"','')
title = title.replace('|','')
title = title.replace('/','')
title = title.replace('\\','')
title = title.replace('*','')
title = title.replace('<',' title="title.replace('">','')
title = title.replace('?','')
#print(title)
file_name = r'E:\code\python\spider_news\sociaty\\' +title + '.txt'
file = open(file_name,'w',encoding = 'utf-8')
file.write(tag.get_text())
if __name__ == '__main__':
urls = ['http://temp.163.com/special/00804KVA/cm_shehui.js?callback=data_callback',
'http://temp.163.com/special/00804KVA/cm_shehui_02.js?callback=data_callback',
'http://temp.163.com/special/00804KVA/cm_shehui_03.js?callback=data_callback']
for url in urls:
#url = 'http://temp.163.com/special/00804KVA/cm_shehui_02.js?callback=data_callback'
req = request.urlopen(url)
res = req.read().decode('gbk')
#print(res)
pat1 = r'"title":"(.*?)",'
pat2 = r'"tlink":"(.*?)",'
m1 = re.findall(pat1,res)
news_title = []
for i in m1:
news_title.append(i)
m2 = re.findall(pat2,res)
news_url = []
for j in m2:
news_url.append(j)
for i in range(0,len(news_url)):
#print(news_title[i],news_body[i])
download(news_title[i],news_url[i])
print('正在爬取第' + str(i) + '个新闻',news_title[i])
通过上述代码,我们就可以获取到网易社会新闻的相关内容了。黑洞代理IP为您提供安全稳定、高效便捷的爬虫代理IP服务,更多问题请点击官网咨询客服。
相关文章内容简介
1 动态IP如何帮助爬虫爬取网易社会新闻内容
网上每天都有大量的新闻内容出现,如何快速获取这些内容呢?今天黑洞代理IP就为大家分享一下,爬取是如何爬取网易的社会新闻内容的。首先我们可以先打开目标页面,右键检查,查找我们需要的内容。这里我们主要抓取新闻标题、链接和新闻内容。 代码如下: import∵re from∵urllib∵import∵request from∵bs4∵import∵BeautifulSoup def∵download... [阅读全文]
最新标签
推荐阅读
20
2019-06
动态ip代理软件
动态ip代理软件,现在做业务越来越难,市面上的动态ip代理软件五花八门,试过很多动态代理ip软件对业务效果并不理想,那么黑洞代理做业务的成功率就会很高,下面我来介绍一下。
24
2019-01
网络爬虫一定使用代理IP软件吗?
许多人觉得网络爬虫一定需要代理ip软件,如果没有了代理ip将无路可走,但也有一部分人认为不一定非需要代理ip,可以用采集工具替代,那么网络爬虫一定使用代理IP软件吗?
15
2019-05
你会用付费ip代理工具吗
ip转换器种类很多,有付费ip代理工具也有免费ip代理工具,这款ip转换工具是一款简单、使用、方便、快捷,操作易上手的ip自动更换工具,是大家日常生活中非常好用的好帮手,可以让您自由
10
2018-10
自动换ip软件哪个好?
很多用户都遇到过这些情况,比如在一些网站上面批量注册帐号、发布留言评论,或在使用一些即时通讯软件频繁发送消息等等这些行为都会遇到系统的ip限制,限制批量注册、发布消息。
热门文章