猫眼电影用senlenuim爬取
senlenuim拿到网页源码——page_source
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# driver.page_source 获取html源代码
html = driver.page_source
print(html)
find()查看html中字符串是否存在
# find() 在html结构中查找某个字符串是否存在
## 5682 数字就证明字符串存在,数字没有规律
## 如果不存在就返回-1
print(driver.page_source.find('kw'))
get_attribute——定位某一标签后查看某一属性值,获取节点属性值
猫眼电影中,找到海报图片链接,是在a标签里面的src
# noed(元素对象) node.get_attribute
# get_attribute 获取数据属性值 定位元素位置,再用attribute获取属性值
# 爬取猫眼电影 https://maoyan.com/board/4
driver.get('https://maoyan.com/board/4')
time.sleep(1)
img_tag =driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/dl/dd[1]/a/img[2]')
# 结果https://p0.meituan.net/movie/414176cfa3fea8bed9b579e9f42766b9686649.jpg@160w_220h_1e_1c
print(img_tag.get_attribute('src'))
爬取猫眼电影top100
分析:数据在dl里面,一个dd标签就是一个电影数据
from selenium import webdriver
import csv
driver =webdriver.Chrome()
# 获取网页
driver.get('https://maoyan.com/board/4')
# 翻页
# <a class="page_4" href="javascript:void(0);" style="cursor: default">4</a>
# <a cla