Python学习笔记之爬取猫眼电影

1.request模块
2.XPath提取数据

import requests
from lxml import etree

获取网页数据

def getonepage():
url = f’https://maoyan.com/board’
# 告诉服务器,我们是浏览器字典
header = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36’}
# 调用
r = requests.get(url, headers=header)
# 返回值
return r.txt

解析数据

def parse(text):
# 初始化,标准化
html = etree.HTML(text)
# 提取我们想要的信息,运用XPath语法
names = html.xpath(’//div[@class=“movie-item-info”/p[@class=“name”]/a/@title’)
releasetimes = html.xpath(’//p[@class=“releasetime”]/text()’)
#字典
item = {}
# zip拉链函数
for name, releasetime in zip(names,releasetimes):
item[‘name’] = name
item[‘releasetime’] = releasetime
# 生成器
yield item

def saveFile(data):
with open(‘movie.josn’, ‘a’, enconding=‘utf-8’) as f:
f.write(data)

text = getonepage()
items = parse(text)
for item in items:
saveFile(item)在这里插入代码片

你可能感兴趣的:(python)