Python爬虫新浪新闻网【使用selenium获取JS加载的动态数据】

1 requests爬取网站html文本

使用requests模块的get方法,直接爬取新浪网站的html文本:需要注意的是

  • res.encoding = "utf-8",而不是res.encoding ('utd-8')
  • get请求得到的响应是requests类型的。它具有.text的方法,来获取整个html的内容
import requests
res = requests.get("https://news.sina.com.cn/china/")
res.encoding = 'utf-8'
print(type(res))
print(res.text[:500])

html文本只显示了前500个字符,结果是:





    
    国内新闻_新闻中心_新浪网






2  BeautifulSoup模块解析HTML文本 
  

使用BeautifulSoup的方法,可以解析HTML的文本信息,下面是一段我们自己写的HTML的字符串,通过BeautifulSoup方法,将该HTML字符串解析为bs4.BeautifulSoup类型,需要注意的是:

  • 这个HTML的字符串格式非常严谨,不能多一个空格,之前打的时候,多了一个空格,然后BeautifulSoup解析的时候解析不成功,显示结果为None。
  • BeautifulSoup解析方法,需要自己添加 html.parser的解析器,否则找不到解析器,打印警告信息。
  • 从结果可以看出:get请求得到的响应是requests类型的;BeautifulSoup解析后返回的是bs4.BeautifulSoup类型的。
  • 运行结果可以看出bs4.BeautifulSoup类型的也有text的方法,而且该方法挺强大的。他可以去除没有用的周边信息,直接获取文本信息。
from bs4 import BeautifulSoup
##读入BeautifulSoup
html_sample = '\
 \
  \
 

hello world

\ This is link1 \ This is link2 \ \ ' soup = BeautifulSoup(html_sample,'html.parser') print(soup) print(soup.text)

结果为:


  hello world This is link1 This is link2  

虽然bs4.BeautifulSoup的text方法很强大,单有时,我们的信息存在特殊的标签之中,如何让才能获取到它们的值?


3 BeautifulSoup常用的方法

(1)找出含有某个标签的方法

  • 使用select找出含有 h1标签的元素 ;因为同一个标签的元素可能很多,因此select回传的是一个list
  • 打[0]只剩下beautifulsoup的一个物件
  • 而如果进阶把后面的文字打出来,就用.text的方法
from bs4 import BeautifulSoup
##读入BeautifulSoup
html_sample = '\
 \
  \
 

hello world

\ This is link1 \ This is link2 \ \ ' soup = BeautifulSoup(html_sample,'html.parser') #使用select找出含有 h1标签的元素 ; header = soup.select('h1') print(header) print(soup.select('h1')[0]) print(soup.select('h1')[0].text) #使用select找出含有 a 标签的元素 links = soup.select('a') print(links) ##如果要将这列表中的元素取出来,就用for循环 for link in links: print(link) print(link.text)

运行结果是:

[

hello world

]

hello world

hello world [This is link1, This is link2] This is link1 This is link1 This is link2 This is link2

(2)找出含有特定CSS属性的元素

  • 使用select找出所有id为title的元素 (id前面需要加#)
  • 使用select找出所有class为link的元素 (class前面需要加.)
  • 使用select找出a tag的href的连结
from bs4 import BeautifulSoup
##读入BeautifulSoup
html_sample = '\
 \
  \
 

hello world

\ This is link1 \ This is link2 \ \ ' soup = BeautifulSoup(html_sample,'html.parser') #使用select找出所有id为title的元素 (id前面需要加#) tit = soup.select("#title") print(tit[0]) #使用select找出所有class为link的元素 (class前面需要加.) #使用select找出a tag的href的连结 for link in soup.select(".link"): print(link) print(link['href'])

运行结果是:

hello world

This is link1 # This is link2 # link2

对于里面含有很多属性的连结,我们可以用类似字典的方式,一一挑选出它的值

x = 'I am a link'
soup1 = BeautifulSoup(x,'html.parser')
print(soup1.select('y')[0]['href'])
print(soup1.select('y')[0]['qoo'])
print(soup1.select('y')[0]['abc'])

结果是:

#
123
u6

4 开始爬取新浪新闻工作

我们要获取的内容是最新新闻的标题,时间,网站链接等信息,但是用之前的requests.get请求获取新浪的国内新闻,发现得到的响应里并没有我们要的class = 'feed-card-item'这一项。今天下午搞了一下午,对于没有任何前端基础的我,简直就是碰壁碰壁再碰壁~,

后经思考发现,这一项是动态的JS页面,不能通过requests.get来获得,所以我们使用selenium,启动浏览器,获取动态信息。

代码如下:

import requests
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://news.sina.com.cn/china/")
data = driver.page_source
soup = BeautifulSoup(data, 'lxml')


for new in soup.select('.feed-card-item'):
    if len(new.select('h2'))>0:
        print(new.select('a')[0]['href'])
        print(new.select('h2')[0].text)

Python爬虫新浪新闻网【使用selenium获取JS加载的动态数据】_第1张图片

运行结果:

https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3439569.shtml
D&G辱华事件 中国普通民众的意见(部分)来了
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3431392.shtml
不出所料 帮D&G“解套”的来了
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3411471.shtml
实拍D&G北京门店:店内无顾客 店外有人"拍照留念"
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2997810.shtml
王伟履新中华全国供销总社 曾在中纪委工作15年
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2997470.shtml
于丹卸任北师大艺术与传媒学院分党委书记
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3355376.shtml
社科院2018年企业社会责任排名 三星领跑外企首位
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2997963.shtml
中方提出世贸组织改革的三个基本原则和五点主张
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2997033.shtml
国家监委和重庆公安局一起查的老虎 多涉一宗罪
https://news.sina.com.cn/o/2018-11-23/doc-ihpevhck3356471.shtml
流失海外的敦煌写经“回乡” 首次现身深圳(图)
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2994183.shtml
刘源披露:这件事 刘少奇在中共中央总负责
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2994814.shtml
她成全国最年轻女性省部级领导 系清华博士(简历)
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3323336.shtml
外媒:中国驻巴基斯坦领馆遇袭 2名签证申请者身亡
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3322989.shtml
北京任命两位副市长 市政府班子再现“一正九副”
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3341319.shtml
长江证券正副董事长双双请辞:尤习贵因年龄原因
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3314641.shtml
北京集中任命12个政府组成部门负责人
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2990016.shtml
林克庆和张家明任北京市副市长(图/简历)
https://news.sina.com.cn/o/2018-11-23/doc-ihpevhck3325822.shtml
这些试图挑战国人底线的人和企业 下场都是这样的
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3299710.shtml
郭宁宁任福建省副省长(图/简历)
https://news.sina.com.cn/c/2018-11-23/doc-ihmutuec2984941.shtml
杜嘉班纳道歉声明只在中国发 海外官方账号无动静
https://news.sina.com.cn/c/2018-11-23/doc-ihpevhck3270092.shtml
刘强东案披露"床单精液" 检方:富翁身份无关本案

这个只是获取了标题和网址。至于文章内容和时间,以及编者和评论数

待续~


 

你可能感兴趣的:(Python爬虫新浪新闻网【使用selenium获取JS加载的动态数据】)