如何使用requests库在Python中高效爬取网站数据?

2026-05-29 02:511阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计486个文字,预计阅读时间需要2分钟。

如何使用requests库在Python中高效爬取网站数据?

requests库是一个简介且简单的处理HTTP请求的第三方库。get()方法是获取网页最常用的方式,其基本使用方法如下:

使用requests库获取HTML页面并将其转换为字符串,需要先进行HTML页面格式的解析。

如何使用requests库在Python中高效爬取网站数据?

requests库是一个简介且简单的处理HTTP请求的第三方库

get()是获取网页最常用的方式,其基本使用方式如下

使用requests库获取HTML页面并将其转换成字符串后,需要进一步解析HTML页面格式,这里我们常用的就是beautifulsoup4库,用于解析和处理HTML和XML

下面这段代码便是爬取百度的信息并简单输出百度的界面信息

import requests from bs4 import BeautifulSoup r=requests.get('www.baidu.com') r.encoding=None result=r.text bs=BeautifulSoup(result,'html.parser') print(bs.title) print(bs.title.text)

import requests from bs4 import BeautifulSoup #用来解决乱码现象,所以编写爬取信息的代码最好带上(输出出现乱码或者UnicodeEncodeError:'gbk'codec can't encode character) import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #用来防止反爬取,可以了解一下 headers={"User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6)",   "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",   "Accept-Language" : "en-us",   "Connection" : "keep-alive",   "Accept-Charset" : "GB2312,utf-8;q=0.7,*;q=0.7" } #获取51job网站的基本信息 r=requests.get('search.51job.com/list/000000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=') r.encoding=r.apparent_encoding result=r.text bs=BeautifulSoup(result,'html.parser') print(bs.prettify()) u1=bs.find_all('u1',attrs={'class':'item_con_list'}) #这部分代码便是我们爬取的目标,51job网站上关于python职业的薪资 print(len(u1)) li=bs.find_all('span',attrs={'class':'t4'}) for l in li: print(l.text)

上面这段代码便是爬取51job网站上的与python相关职业的薪资

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计486个文字,预计阅读时间需要2分钟。

如何使用requests库在Python中高效爬取网站数据?

requests库是一个简介且简单的处理HTTP请求的第三方库。get()方法是获取网页最常用的方式,其基本使用方法如下:

使用requests库获取HTML页面并将其转换为字符串,需要先进行HTML页面格式的解析。

如何使用requests库在Python中高效爬取网站数据?

requests库是一个简介且简单的处理HTTP请求的第三方库

get()是获取网页最常用的方式,其基本使用方式如下

使用requests库获取HTML页面并将其转换成字符串后,需要进一步解析HTML页面格式,这里我们常用的就是beautifulsoup4库,用于解析和处理HTML和XML

下面这段代码便是爬取百度的信息并简单输出百度的界面信息

import requests from bs4 import BeautifulSoup r=requests.get('www.baidu.com') r.encoding=None result=r.text bs=BeautifulSoup(result,'html.parser') print(bs.title) print(bs.title.text)

import requests from bs4 import BeautifulSoup #用来解决乱码现象,所以编写爬取信息的代码最好带上(输出出现乱码或者UnicodeEncodeError:'gbk'codec can't encode character) import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #用来防止反爬取,可以了解一下 headers={"User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6)",   "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",   "Accept-Language" : "en-us",   "Connection" : "keep-alive",   "Accept-Charset" : "GB2312,utf-8;q=0.7,*;q=0.7" } #获取51job网站的基本信息 r=requests.get('search.51job.com/list/000000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=') r.encoding=r.apparent_encoding result=r.text bs=BeautifulSoup(result,'html.parser') print(bs.prettify()) u1=bs.find_all('u1',attrs={'class':'item_con_list'}) #这部分代码便是我们爬取的目标,51job网站上关于python职业的薪资 print(len(u1)) li=bs.find_all('span',attrs={'class':'t4'}) for l in li: print(l.text)

上面这段代码便是爬取51job网站上的与python相关职业的薪资

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。