爬虫之路
爬虫的一周学习计划:
下图是爬虫的准备
爬虫爬取快代理案例:
网站的url=“https://www.kuaidaili.com/free/”
这次爬取我们采用的是requests第三方库
Requests 是一个 Python 的 HTTP 客户端库,我们可以用它得到HTML源码
import requests
url="https://www.kuaidaili.com/free/"
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
}
#这里进行了头部的伪装
res=requests.get(url,headers=headers)
res.encoding="utf-8"
html=res.text
之后我们用xpath实现标签的遍历获取到我们需要的内容
e=etree.HTML(html)
ip_list=e.xpath("//tr/td[1]/text()")
port_list=e.xpath("//tr/td[2]/text()")
#采用zip迭代的方式打印输出
for ip,port in zip(ip_list,port_list):
str="ip:"+ip+"\t端口号:"+port
print(str)
小结
本文主要讲解了网络爬虫的结构和应用,以及Python实现爬虫的案例。希望大家对本文中的网络爬虫工作流程和Requests实现HTTP请求的方式重点吸收消化。
转载:https://blog.csdn.net/IT6848/article/details/108733841
查看评论