教程:Python爬虫入门实战七:使用Selenium--以抓取QQ空间好友说说为例

优采云 发布时间: 2022-10-08 10:31

  教程:Python爬虫入门实战七:使用Selenium--以抓取QQ空间好友说说为例

  前面我们谈到了使用请求+BeautifulSoup组合的请求和数据解析来统计网页,如果JS生成的内容,也是通过寻找API获取数据的借口引入的。

  但是,有时网页数据是由JS生成的,找不到API借口或API借口地址随机变化,时间不等人。那么只能使用硒。

  一、硒介绍

  Selenium是一种用于Web应用程序的功能自动化测试工具,它直接在浏览器中运行,就好像一个真正的用户在做这件事一样。

  由于这种性质,Selenium也是一个功能强大的Web数据采集工具,允许浏览器自动加载页面,获取所需的数据,甚至截取页面的屏幕截图,或判断网站上是否发生了某些操作。

  硒没有自己的浏览器,需要与第三方浏览器一起使用。支持的浏览器包括浏览器,火狐,IE,幻影等。

  如果我们使用Chrome,FireFox或IE,我们可以看到浏览器窗口打开,打开网站,然后在代码中执行操作。

  不过,让程序在后台运行,更符合我们爬虫的气质,所以我们用幻影作为浏览器载体,而这个文章也引入了幻影。

  Phantomjs是一个“无头”浏览器,即没有界面的浏览器,但功能与普通浏览器没有什么不同。

  二、用蟒蛇里的硒让QQ空间的朋友说

  在使用pip安装硒之前,可以直接在代码中导入。

  我们来举个实际的例子——获取一个QQ空间好友的谈话信息,简单讲解一下硒+幻影的用法。

  我们需要像这样抓取页面:

  在QQ空间与朋友交谈的链接是:{朋友QQ号码}/311

  我们抓住了他演讲的时间和内容。

  仍然首先在代码上:

  from bs4 import BeautifulSoup

from selenium import webdriver

import time

#使用selenium

driver = webdriver.PhantomJS(executable_path="D:\\phantomjs.exe")

driver.maximize_window()

#登录QQ空间

def get_shuoshuo(qq):

driver.get('http://user.qzone.qq.com/{}/311'.format(qq))

time.sleep(5)

try:

driver.find_element_by_id('login_div')

a = True

except:

a = False

if a == True:

driver.switch_to.frame('login_frame')

driver.find_element_by_id('switcher_plogin').click()

driver.find_element_by_id('u').clear()#选择用户名框

driver.find_element_by_id('u').send_keys('QQ号')

driver.find_element_by_id('p').clear()

driver.find_element_by_id('p').send_keys('QQ密码')

driver.find_element_by_id('login_button').click()

time.sleep(3)

driver.implicitly_wait(3)

try:

driver.find_element_by_id('QM_OwnerInfo_Icon')

b = True

except:

b = False

<p>

if b == True:

driver.switch_to.frame(&#39;app_canvas_frame&#39;)

content = driver.find_elements_by_css_selector(&#39;.content&#39;)

stime = driver.find_elements_by_css_selector(&#39;.c_tx.c_tx3.goDetail&#39;)

for con,sti in zip(content,stime):

data = {

&#39;time&#39;:sti.text,

&#39;shuos&#39;:con.text

}

print(data)

pages = driver.page_source

soup = BeautifulSoup(pages,&#39;lxml&#39;)

cookie = driver.get_cookies()

cookie_dict = []

for c in cookie:

ck = "{0}={1};".format(c[&#39;name&#39;],c[&#39;value&#39;])

cookie_dict.append(ck)

i = &#39;&#39;

for c in cookie_dict:

i += c

print(&#39;Cookies:&#39;,i)

print("==========完成================")

driver.close()

driver.quit()

if __name__ == &#39;__main__&#39;:

get_shuoshuo(&#39;好友QQ号&#39;)</p>

  获得的一些数据如下:

  接下来,让我们通过解释代码来看看硒的使用

  三、代码分析

  1. 像往常一样,导入您需要使用的模块:

  from bs4 import BeautifulSoup

from selenium import webdriver

import time

  2. 使用硒的 Web 驱动程序实例化浏览器对象,其中幻影 :

  driver = webdriver.PhantomJS(executable_path="D:\\phantomjs.exe")

  3. 设置幻影窗口最大化:

  driver.maximize_window()

  4.主要功能部分

  使用 get() 方法打开要抓取的网址:

  driver.get(&#39;http://user.qzone.qq.com/{}/311&#39;.format(qq))

  等待5秒后,判断页面是否需要登录,通过查找页面是否具有对应的DIV ID进行判断:

   try:

driver.find_element_by_id(&#39;login_div&#39;)

a = True

<p>

except:

a = False</p>

  如果页*敏*感*词*有用于登录的 DIV,则模拟登录:

   driver.switch_to.frame(&#39;login_frame&#39;) #切换到登录ifram

driver.find_element_by_id(&#39;switcher_plogin&#39;).click()

driver.find_element_by_id(&#39;u&#39;).clear()#选择用户名框

driver.find_element_by_id(&#39;u&#39;).send_keys(&#39;QQ号&#39;)

driver.find_element_by_id(&#39;p&#39;).clear()#选择密码框

driver.find_element_by_id(&#39;p&#39;).send_keys(&#39;QQ密码&#39;)

driver.find_element_by_id(&#39;login_button&#39;).click()#点击登录按钮

time.sleep(3)

  接下来,通过确定是否存在元素 ID 来确定友元空间是否设置了权限:QM_OwnerInfo_Icon

   try:

driver.find_element_by_id(&#39;QM_OwnerInfo_Icon&#39;)

b = True

except:

b = False

  如果您有权访问讨论页,请找到元素和数据并解析它们:

   if b == True:

driver.switch_to.frame(&#39;app_canvas_frame&#39;)

content = driver.find_elements_by_css_selector(&#39;.content&#39;)

stime = driver.find_elements_by_css_selector(&#39;.c_tx.c_tx3.goDetail&#39;)

for con,sti in zip(content,stime):

data = {

# &#39;qq&#39;:qq,

&#39;time&#39;:sti.text,

&#39;shuos&#39;:con.text

}

print(data)

  除了解析硒中的数据之外,我们还可以将当前页面保存为源代码,并使用美丽汤来解析它:

  pages = driver.page_source

soup = BeautifulSoup(pages,&#39;lxml&#39;)

  最后,让我们尝试使用get_cookies()获取一个饼干:

   cookie = driver.get_cookies()

cookie_dict = []

for c in cookie:

ck = "{0}={1};".format(c[&#39;name&#39;],c[&#39;value&#39;])

cookie_dict.append(ck)

i = &#39;&#39;

for c in cookie_dict:

i += c

print(&#39;Cookies:&#39;,i)

  此外,还介绍了两种比较常见的硒方法:

  保存截图:

  driver.save_screenshot(&#39;保存的文件路径及文件名&#39;)

  执行 JS 脚本:

  driver.execute_script("JS代码")

  有关硒的更详细的操作和使用,可以在线搜索推荐的书籍“硒网络驱动程序(python)第三版”。

  解读:自动抓取wordpress 了解更多有关自动抓取wordpress的内容

  WordPress教育培训学校网站免费主题模板推荐

  

  字压轴承网站免费主题模板推荐

  

  WordPress 6.0发布,具有完全增强的编辑器和站点范围的编辑

  修复wordpress搜索框中空白无条件搜索结果仍然可用的问题

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线