赞
踩
爬虫库的作用就是获取网页源代码,学习之前需要学习数据分析——Python网络爬虫(二){Http基本原理}
Python内置的http请求库,包括如下模块:
#get请求,访问百度
import urllib.request
response=urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))#decode就是解码后的响应
response.status #响应状态
response.getheaders() #响应头
获取网站源代码后 ,可以得知,网站的响应码,以及网站的响应头
#post请求
import urllib.parse
import urllib.request
da = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8')
#urlencode方法将参数字典转换为字符串
response = urllib.request.urlopen('http://httpbin.org/post',data=da)
print(response.read())
#Request,可以加headers信息
import urllib.request
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
request = urllib.request.Request('http://www.baidu.com',headers=headers)
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
headers详见数据分析——Python网络爬虫(二){Http基本原理}
案例一:提取链家房源图片
## 获取网页的源代码 url='https://tj.lianjia.com/ditiezufang/li110458004/' headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'} request = urllib.request.Request(url,headers=headers) response = urllib.request.urlopen(request) if response.status==200: #判断是否正常响应 html=response.read().decode('utf-8') ## 编写正则表达式 import re reg='data-src="(.*o_auto|.*\.jpg)"\n'#源代码格式图片 imgre=re.compile(reg) imglist = imgre.findall(html) ## 保存到本地数据库 import os os.makedirs('C:\\Users\\90541\\Desktop\\数据分析\\pycode\\picture') #指定路径下创建目录 os.chdir('C:\\Users\\90541\\Desktop\\数据分析\\pycode\\picture')# 工作路径指向这个目录 x=1 for img in imglist: img=img.replace('250x182','780x439') urllib.request.urlretrieve(img,'%s.jpg' % x)#直接将远程数据下载到本地 x+=1
案例二:豆瓣电影分类排行榜(JSON数据格式)
涉及到爬取多页内容
# 获取网页的源代码
import urllib
url='https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start=0&limit=20'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
request = urllib.request.Request(url,headers=headers)
response = urllib.request.urlopen(request)
html=response.read().decode('utf-8')
# 因为是JSON格式,需要要用padas对JSON格式进行解析
import pandas as pd
from io import StringIO
df = pd.read_json(StringIO(html))
# 获取到了最原始的JSON格式
#挑选主要信息
df[['rank','rating','title','actors']].set_index('rank')
上面仅仅是获取单页的,下面的就来尝试获取多页的
根据URL可以看出,每一页的变化为 <start部分有了数字变化>
1. https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start=0&limit=20
2. https://movie.douban.com/j/chart/top_listtype=25&interval_id=100%3A90&action=&start=20&limit=20
3. https://movie.douban.com/j/chart/top_listtype=25&interval_id=100%3A90&action=&start=40&limit=20
4. https://movie.douban.com/j/chart/top_listtype=25&interval_id=100%3A90&action=&start=60&limit=20
import time import pandas as pd import random from io import StringIO data=pd.DataFrame() for i in range(7): print('正在爬取第%d页'%i) i=i*20 baseurl='https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start' #url前面不变的地方 url = baseurl+str(i)+'&limit=20' headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'} request = urllib.request.Request(url,headers=headers) response = urllib.request.urlopen(request) html=response.read().decode('utf-8') df = pd.read_json(StringIO(html)) data=pd.concat([data,df]) time.sleep(random.randint(6,8))#每爬取一次,随机休息 print('爬取完毕') data[['rank','rating','title','actors']].set_index('rank')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。