模拟登陆豆瓣并爬去豆瓣主页的部分数据

该文章主要使用selenium模拟登陆豆瓣,并且提取主页的部分信息,熟练使用selenium

模拟登陆豆瓣并爬去豆瓣主页的部分数据

该文章主要使用selenium模拟登陆豆瓣,并且提取主页的部分信息。

selenium的安装以及基本使用在前面的文章有介绍。

功能要求:

1、模拟登陆豆瓣

2、爬去主页的部分信息并打印出来

程序代码:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   模拟登陆豆瓣.py
@Time    :   2019/02/23 16:20:11
@Author  :   My.Yong 
@Version :   1.0
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

# here put the import lib
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time,requests
from bs4 import BeautifulSoup

driver = webdriver.PhantomJS(executable_path='D:/phantomjs/bin/phantomjs.exe')
driver.get('https://accounts.douban.com/passport/login_popup?login_source=anony')
#模拟点击切换 选择密码登陆
tab = driver.find_element_by_xpath('//li[@class="account-tab-account"]')
#在 tab 位置单击
ActionChains(driver).move_to_element(tab).click(tab).perform()

#输入账号密码
driver.find_element_by_id('username').send_keys('xxxxxxx')
driver.find_element_by_id('password').send_keys('xxxxxxxx')

#模拟点击登陆
driver.find_element_by_xpath('//div[@id="tmpl_phone"]/div[@class="account-form-field-submit "]/a').click()

#等待3秒
time.sleep(1)

base_url = 'https://www.douban.com/'
driver.get(base_url)

html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
nodes = soup.select('.status-wrapper')
for node in nodes:
    text = node.select('.content p')
    text = text[0].get_text() if len(text) > 0 else 0
    print(text)
#for item in nodes:
#    print(item)
#response = requests.get(another_url, headers=headers)
#print(response.content)
#生产登陆后快照
driver.save_screenshot('douban.png')
driver.quit()
上一篇:整理一波PHP计算时间的方法 下一篇:嘉瑞XMES2.0信息化管理系统

评论

评论(0)

暂无评论