python的爬虫(3)抓取全部链接+标题

前言:
先抓取一页数据-》查看信息之所以查看更多是因为分页-》所以可以先抓取一页数据,再循环页码就可以得到全部数据

被抓地址:http://jhsjk.people.cn/result/?area=402
分了3页

在这里插入图片描述
抓取1页数据所有的标题
在这里插入图片描述
在这里插入图片描述

import requests
from lxml import etree
# 页码的分页
r=requests.get('http://jhsjk.people.cn/result/?area=402').content

topic=etree.HTML(r)
title=topic.xpath('//div/ul/li/a/text()')
print(title)

``
==========翻页
分析
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190528174909449.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTY2NTYzNw==,size_16,color_FFFFFF,t_70)

翻页
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190528175121653.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTY2NTYzNw==,size_16,color_FFFFFF,t_70)
运用到代码中

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190528175527736.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTY2NTYzNw==,size_16,color_FFFFFF,t_70)

import requests
from lxml import etree

for a in range(1,4):
m=‘http://jhsjk.people.cn/result/’+str(a)+’?area=402
l=requests.get(m).content
topic=etree.HTML(l)

title=topic.xpath('//div/ul/li/a/text()')
for x in title:
     print(x)

猜你喜欢

转载自blog.csdn.net/weixin_41665637/article/details/90644280