luzihang avatar

python爬虫基础篇

luzihang

Published: 18 Apr 2020 › Updated: 18 Apr 2020python爬虫基础篇

python爬虫基础篇


简介

python爬虫是一种非常方便实用的一种方式用来抓取网页信息以及数据,接下来我通过一个非常简单基础的爬取豆瓣电影实例来体会其中的强大之处。

准备工作

Pycharm IDE

requests, lxml库

xpath语法

首先就是导入requests,lxml库

image.png

由于每个网站都会有一定的反爬虫机制,在爬取时候需要做一些处理去构造用户标识头User-Agent

image.png这里通过requests请求获取网页源代码,然后用lxml+xpath对网页的数据进行分析

image.png

在网页中右键检查就可以查看网页完整的源代码,这里通过xpath的语法找到电影信息在li标签下面,可以直接通过//ul[classHive account@class='lists']/li找到对应的信息

image.png

最后通过遍历得到我们所需要的爬虫数据并且打印出来image.png

打印结果

成功输出了得到的电影数据

image.png

Leave python爬虫基础篇 to:

Written by

Read more #cn-malaysia posts


Best Posts From luzihang

We have not curated any of luzihang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From luzihang