当前位置:   article > 正文

Python Flask定时调度疫情大数据爬取全栈项目实战使用-15爬取热搜数据_flask定时爬取疫情数据

flask定时爬取疫情数据

爬取热搜数据

selenium介绍

对于使用了动态渲染技术,我们可以使用selenium来爬取

selenium是一个用于web应用程序测试的工具,直接运行在浏览器中,就像整整的用户在操作一样。

selenium安装

1.安装:

pip install selenium
  • 1

Successfully installed selenium-3.141.0

2.安装浏览器(谷歌)

产看当前chrome版本:

在这里插入图片描述

3.下载对应的浏览器驱动:

  1. 创建浏览器对象
  2. 浏览器.get()
  3. 浏览器.find()

驱动下载地址:http://npm.taobao.org/mirrors/chromedriver/

4.下载完成后

防止到对应的文件夹下:
在这里插入图片描述

5.启动selenium自动化控制Chrome浏览器

from selenium.webdriver import Chrome,ChromeOptions

brower = Chrome()
  • 1
  • 2
  • 3

在这里插入图片描述

6.通过url拿到源码地址

from selenium.webdriver import Chrome,ChromeOptions

browser = Chrome()
url = "https://space.bilibili.com/473837611"
browser.get(url)
print(browser.page_source)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

7.确定xpath路径

7.1copy xpath

在这里插入图片描述

7.2搜索确定

在这里插入图片描述

//*[@id="page-index"]/div[1]/div[2]/div/div/a[2]
  • 1

8.验证数据 打印出获取到的浏览器对象

from selenium.webdriver import Chrome,ChromeOptions

browser = Chrome()
url = "https://space.bilibili.com/473837611"
browser.get(url)
#print(browser.page_source)
c = browser.find_elements_by_xpath('//*[@id="page-index"]/div[1]/div[2]/div/div/a[2]')
print(c)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

9.获取数据中所有的标题

from selenium.webdriver import Chrome,ChromeOptions

browser = Chrome()
url = "https://space.bilibili.com/473837611"
browser.get(url)
#print(browser.page_source)
c = browser.find_elements_by_xpath('//*[@id="page-index"]/div[1]/div[2]/div/div/a[2]')
for i in c:
    print(i.text)
browser.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

10.打开chrome无头模式

from selenium.webdriver import Chrome,ChromeOptions
option = ChromeOptions()
option.add_argument("--headless") #隐藏浏览器
option.add_argument("--no-sandbox") #Linux去除沙盒
browser = Chrome(options=option)
url = "https://space.bilibili.com/473837611"
browser.get(url)
#print(browser.page_source)
c = browser.find_elements_by_xpath('//*[@id="page-index"]/div[1]/div[2]/div/div/a[2]')
for i in c:
    print(i.text)
browser.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/143347
推荐阅读
相关标签
  

闽ICP备14008679号