⑴ python爬取新浪网页新闻时,分割代码类名怎么消失了
建议改用属性查找,如下示例:
soup.find(attrs={‘class’:’feed-card-item’})
图3替换是什么意思?请补充描述。
⑵ 怎么用Python网络爬虫爬取腾讯新闻内容
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地。 类似于使用程序模拟IE浏览器的功能,把URL作为HTTP请求的内容发送到服务器端, 然后读取服务器端的响应资源。 在Python中,我们使用urllib2这个组件来抓取网页。u...
⑶ python3 怎么爬取新闻网站
1 #coding=utf-8
2 import re # 正则表达式
3 import bs4 # Beautiful Soup 4 解析模块
4 import urllib2 # 网络访问模块
5 import News #自己定义的新闻结构
6 import codecs #解决编码问题的关键 ,使用codecs.open打开文件
7 import sys #1解决不同页面编码问题
8
9 reload(sys) # 2
10 sys.setdefaultencoding('utf-8') # 3
11
12 # 从首页获取所有链接
13 def GetAllUrl(home):
14 html = urllib2.urlopen(home).read().decode('utf8')
15 soup = bs4.BeautifulSoup(html, 'html.parser')
16 pattern = 'http://\w+\.jia\.\.com/article/\w+'
17 links = soup.find_all('a', href=re.compile(pattern))
18 for link in links:
19 url_set.add(link['href'])
20
21 def GetNews(url):
22 global NewsCount,MaxNewsCount #全局记录新闻数量
23 while len(url_set) != 0:
24 try:
25 # 获取链接
26 url = url_set.pop()
27 url_old.add(url)
28
29 # 获取代码
30 html = urllib2.urlopen(url).read().decode('utf8')
31
32 # 解析
33 soup = bs4.BeautifulSoup(html, 'html.parser')
34 pattern = 'http://\w+\.jia\.\.com/article/\w+' # 链接匹配规则
35 links = soup.find_all('a', href=re.compile(pattern))
36
37 # 获取URL
38 for link in links:
39 if link['href'] not in url_old:
40 url_set.add(link['href'])
41
42 # 获取信息
43 article = News.News()
44 article.url = url # URL信息
45 page = soup.find('div', {'id': 'page'})
46 article.title = page.find('h1').get_text() # 标题信息
47 info = page.find('div', {'class': 'article-info'})
48 article.author = info.find('a', {'class': 'name'}).get_text() # 作者信息
49 article.date = info.find('span', {'class': 'time'}).get_text() # 日期信息
50 article.about = page.find('blockquote').get_text()
51 pnode = page.find('div', {'class': 'article-detail'}).find_all('p')
52 article.content = ''
53 for node in pnode: # 获取文章段落
54 article.content += node.get_text() + '\n' # 追加段落信息
55
56 SaveNews(article)
57
58 print NewsCount
59 break
60 except Exception as e:
61 print(e)
62 continue
63 else:
64 print(article.title)
65 NewsCount+=1
66 finally:
67 # 判断数据是否收集完成
68 if NewsCount == MaxNewsCount:
69 break
70
71 def SaveNews(Object):
72 file.write("【"+Object.title+"】"+"\t")
73 file.write(Object.author+"\t"+Object.date+"\n")
74 file.write(Object.content+"\n"+"\n")
75
76 url_set = set() # url集合
77 url_old = set() # 爬过的url集合
78
79 NewsCount = 0
80 MaxNewsCount=3
81
82 home = 'http://jia..com/' # 起始位置
83
84 GetAllUrl(home)
85
86 file=codecs.open("D:\\test.txt","a+") #文件操作
87
88 for url in url_set:
89 GetNews(url)
90 # 判断数据是否收集完成
91 if NewsCount == MaxNewsCount:
92 break
93
94 file.close()
复制代码
新闻文章结构
复制代码
1 #coding: utf-8
2 # 文章类定义
3 class News(object):
4 def __init__(self):
5 self.url = None
6 self.title = None
7 self.author = None
8 self.date = None
9 self.about = None
10 self.content = None
⑷ 如何用Python爬虫抓取网页内容
首先,你要安装requests和BeautifulSoup4,然后执行如下代码.
importrequests
frombs4importBeautifulSoup
iurl='http://news.sina.com.cn/c/nd/2017-08-03/doc-ifyitapp0128744.shtml'
res=requests.get(iurl)
res.encoding='utf-8'
#print(len(res.text))
soup=BeautifulSoup(res.text,'html.parser')
#标题
H1=soup.select('#artibodyTitle')[0].text
#来源
time_source=soup.select('.time-source')[0].text
#来源
origin=soup.select('#artibodyp')[0].text.strip()
#原标题
oriTitle=soup.select('#artibodyp')[1].text.strip()
#内容
raw_content=soup.select('#artibodyp')[2:19]
content=[]
forparagraphinraw_content:
content.append(paragraph.text.strip())
'@'.join(content)
#责任编辑
ae=soup.select('.article-editor')[0].text
这样就可以了
⑸ Python如何简单爬取腾讯新闻网前五页文字内容
可以使用python里面的一个爬虫库,beautifulsoup,这个库可以很方便的爬取数据。爬虫首先就得知道网页的链接,然后获取网页的源代码,通过正则表达式或者其他方法来获取所需要的内容,具体还是要对着网页源代码进行操作,查看需要哪些地方的数据,然后通过beautifulsoup来爬取特定html标签的内容。网上有很多相关的内容,可以看看。
⑹ 如何利用python爬取网页内容
利用python爬取网页内容需要用scrapy(爬虫框架),但是很简单,就三步
定义item类
开发spider类
开发pipeline
想学习更深的爬虫,可以用《疯狂python讲义》
⑺ python爬虫如何分析一个将要爬取的网站
首先,你去爬取一个网站,
你会清楚这个网站是属于什么类型的网站(新闻,论坛,贴吧等等)。
你会清楚你需要哪部分的数据。
你需要去想需要的数据你将如何编写表达式去解析。
你会碰到各种反爬措施,无非就是各种网络各种解决。当爬取成本高于数据成本,你会选择放弃。
你会利用你所学各种语言去解决你将要碰到的问题,利用各种语言的client组件去请求你想要爬取的URL,获取到HTML,利用正则,XPATH去解析你想要的数据,然后利用sql存储各类数据库。
⑻ python3 怎么爬取新闻网站
从门户网站爬取新闻,将新闻标题,作者,时间,内容保存到本地txt中。
importre#正则表达式
importbs4#BeautifulSoup4解析模块
importurllib2#网络访问模块
importNews#自己定义的新闻结构
importcodecs#解决编码问题的关键,使用codecs.open打开文件
importsys#1解决不同页面编码问题
其中bs4需要自己装一下,安装方法可以参考:Windows命令行下pip安装python whl包
程序:
#coding=utf-8
importre#正则表达式
importbs4#BeautifulSoup4解析模块
importurllib2#网络访问模块
importNews#自己定义的新闻结构
importcodecs#解决编码问题的关键,使用codecs.open打开文件
importsys#1解决不同页面编码问题
reload(sys)#2
sys.setdefaultencoding('utf-8')#3
#从首页获取所有链接
defGetAllUrl(home):
html=urllib2.urlopen(home).read().decode('utf8')
soup=bs4.BeautifulSoup(html,'html.parser')
pattern='http://w+.jia..com/article/w+'
links=soup.find_all('a',href=re.compile(pattern))
forlinkinlinks:
url_set.add(link['href'])
defGetNews(url):
globalNewsCount,MaxNewsCount#全局记录新闻数量
whilelen(url_set)!=0:
try:
#获取链接
url=url_set.pop()
url_old.add(url)
#获取代码
html=urllib2.urlopen(url).read().decode('utf8')
#解析
soup=bs4.BeautifulSoup(html,'html.parser')
pattern='http://w+.jia..com/article/w+'#链接匹配规则
links=soup.find_all('a',href=re.compile(pattern))
#获取URL
forlinkinlinks:
iflink['href']notinurl_old:
url_set.add(link['href'])
#获取信息
article=News.News()
article.url=url#URL信息
page=soup.find('div',{'id':'page'})
article.title=page.find('h1').get_text()#标题信息
info=page.find('div',{'class':'article-info'})
article.author=info.find('a',{'class':'name'}).get_text()#作者信息
article.date=info.find('span',{'class':'time'}).get_text()#日期信息
article.about=page.find('blockquote').get_text()
pnode=page.find('div',{'class':'article-detail'}).find_all('p')
article.content=''
fornodeinpnode:#获取文章段落
article.content+=node.get_text()+' '#追加段落信息
SaveNews(article)
printNewsCount
break
exceptExceptionase:
print(e)
continue
else:
print(article.title)
NewsCount+=1
finally:
#判断数据是否收集完成
ifNewsCount==MaxNewsCount:
break
defSaveNews(Object):
file.write("【"+Object.title+"】"+" ")
file.write(Object.author+" "+Object.date+" ")
file.write(Object.content+" "+" ")
url_set=set()#url集合
url_old=set()#爬过的url集合
NewsCount=0
MaxNewsCount=3
home='http://jia..com/'#起始位置
GetAllUrl(home)
file=codecs.open("D:\test.txt","a+")#文件操作
forurlinurl_set:
GetNews(url)
#判断数据是否收集完成
ifNewsCount==MaxNewsCount:
break
file.close()
新闻文章结构
#coding:utf-8
#文章类定义
classNews(object):
def__init__(self):
self.url=None
self.title=None
self.author=None
self.date=None
self.about=None
self.content=None
对爬取的文章数量就行统计。