selenium结合lxml爬取豆瓣电影相关信息
来源:互联网 发布:电脑淘宝怎么延长收货 编辑:程序博客网 时间:2024/05/22 11:46
- 环境说明
- 重要代码解释
- 完整代码
环境说明
python3.5
centos7.2
重要代码解释
使用selenium加载网页:
driver=webdriver.PhantomJS()driver.get("https://movie.douban.com/")
使用selenium和web进行互动将网页加在完全:
end = Truewhile (end): try: end = driver.find_element_by_class_name("more") end.click() except Exception as e: print("没有这样的text.") end = False
获得电影信息的web的源代码:
movis = driver.page_sourcedriver.close()
使用xpath解析web代码:
html = etree.HTML(movis)titles = html.xpath("//a[@class='item']")
提取需要的内容:
i =0while(i<len(titles)): url_img = titles[i].xpath("./div/img/@src") title_moive = titles[i].xpath("./p/text()") rank_movie = titles[i].xpath("./p/strong/text()") title_moive=re.sub("\s+","",title_moive[0]) i= i+1
完整代码
from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom scrapy.selector import Selectorfrom lxml import etreeimport redriver=webdriver.PhantomJS()driver.get("https://movie.douban.com/")end = Truewhile (end): try: end = driver.find_element_by_class_name("more") end.click() except Exception as e: print("没有这样的text.") end = Falsemovis = driver.page_sourcedriver.close()print(type(movis))html = etree.HTML(movis)titles = html.xpath("//a[@class='item']")i =0while(i<len(titles)): url_img = titles[i].xpath("./div/img/@src") title_moive = titles[i].xpath("./p/text()") rank_movie = titles[i].xpath("./p/strong/text()") title_moive=re.sub("\s+","",title_moive[0]) i= i+1 print(url_img,"===",title_moive,"===",rank_movie) print("****************************************************************************")
0 0
- selenium结合lxml爬取豆瓣电影相关信息
- BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- python中lxml+cssselect爬取豆瓣电影Top250
- [python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- 爬取豆瓣网电影信息
- python爬取豆瓣电影信息
- scrpy 豆瓣电影信息爬取
- python爬取豆瓣电影信息
- nodejs爬取豆瓣top250电影信息
- 基于BeautifulSoup爬取豆瓣网上的电影信息
- 豆瓣电影信息爬取并保存到excel
- 【scrapy】scrapy按分类爬取豆瓣电影基础信息
- 使用scrapy框架爬取豆瓣电影top250信息
- python3实现豆瓣top250电影信息爬取
- Python爬虫入门 | 2 爬取豆瓣电影信息
- 爬取豆瓣的电影
- Python爬取豆瓣电影
- 爬取豆瓣电影数据
- HDU 3336 Count the string
- android 内置apk 卸载后开机自动恢复的实现
- wince镜像文件的区别
- C++ enum用法
- CSU-1804 有向无环图(拓扑排序+dp)(湖南省第十二届大学生程序设计竞赛)
- selenium结合lxml爬取豆瓣电影相关信息
- 微信开发之----封装微信红包类
- TCP协议的三次握手与四次挥手
- linux c++ 服务器端开发面试必看书籍
- Javascript 中的神器——Promise
- 勾股定理一日一证连载92
- 数据库中的索引优缺点分析
- Redis 集群教程
- Git秘籍:在 Git 中进行版本回退