欢迎关注公众号K的笔记阅读博主更多优质学习内容
K的笔记公众号

上一篇内容:Python爬虫初级(一)—— Requests 库入门
前面我们讲了网络爬虫常用库——Requests,下面我们直接通过几个实例实现网络爬虫:

实例一:京东商品页面的爬取

首先我们打开京东页面选择商品:
https://item.jd.com/100008348530.html,我们要做的事情是通过网络爬虫获取该商品的有关信息,该页面内容如下:
京东商品 Apple iphone 11 下面我们对网页进行简单爬取测试:

import requests
r = requests.get("https://item.jd.com/100008348530.html")
print(r.statue_code) 		# 返回200
print(r.encoding)     		# 返回'gbk'
r.text[:1000]				# 返回了正确内容

测试基本正常,我们按照前面的文章所说的爬虫通用框架对网页进行爬取:

import requests
url = "https://item.jd.com/100008348530.html"
try:
    r = requests.get(url)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    print(r.text)
except:
    print("爬取失败")

实例二:亚马逊商品页面的爬取

我们首先访问页面 https://www.amazon.cn/dp/B0785D5L1H/ref=sr_1_1,下面直接通过代码实现对商品信息的爬取:

import requests
try:
    r = requests.get("https://www.amazon.cn/dp/B0785D5L1H/ref=sr_1_1")
    r.raise_for_status()
    r.encoding = apparent_encoding
    print(r.text[:100])
except:
    print("爬取错误")
    print(r.status_code)
# 返回 503

由此例我们可见,亚马逊对我们的爬虫有限制,我们输入以下命令:

r.request.headers
# {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

我们发现在 ‘User-Agent’ 字段下,我们的爬虫忠实地告诉了亚马逊地服务器,这次访问是由 python 的 requests 库发起的,因此我们需要模拟浏览器的请求:

import requests
url = "https://www.amazon.cn/dp/B0785D5L1H/ref=sr_1_1"
try:
	kv = {'user-agent':'Mozilla/5.0'}
	r = requests.get(url, headers=kv)
	r.raise_for_status()
	r.encoding = r.apparent_encoding
	print(r.text[1000:2000])
except:
	print("爬取失败")

此时已成功爬取到数据

实例三:百度/360搜索关键词提交

首先我们观察一下百度和360的搜索接口:

百度的关键词接口: http://www.baidu.com/s?wd=keyword
360的关键词接口:http://www.so.com/s?q=keyword

也就是在这两个接口中,我们只需要将关键词代入 keyword,即可实现提交关键词了,下面我们用 requests 实现相关代码:

import requests
kv = {"wd":"python"}
r = requests.get("http://www.baidu.com/s",params=kv)
print(r.status_code)
print(r.request.url)

若无意外状态码返回值为200,而后面的 url 返回的是一个百度安全验证的东西,360搜索的返回值则正常。

实例四:网络图片的爬取和存储

首先我们看一下网络图片链接的格式:

http://www.example.com/picture.jpg

那么我们应该怎样进行图片存储呢?

import requests
import os
root = "D://Temp//Python//Spider//Spider_lecture//Real_test//"
url = "http://img0.dili360.com/pic/2019/05/13/5cd93370871d19s75711409_t.jpg"
path = root + url.split("/")[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r = requests.get(url)
        with open(path, "wb") as f:
            f.write(r.content)
            f.close()
            print("文件保存成功 ")
    else:
        print("文件已存在")
except:
    print("爬取失败")

实例五:IP地址归属地的自动查询

当给出一个 IP 地址,我们要怎么判断这个地址是在北京呢,还是在广州呢?事实上,我们有一个网站可以查询相关数据:www.ip138.com,下图即为该网站的界面:
IP地址查询网站界面那么我们应该怎样通过爬虫进行自动实现呢?事实上,我们通过输入不同的 URL 链接可以发现这样一个规律,即该网站的 URL 组成为:

http://www.ip138.com/iplookup.asp?ip=ipaddress&action=2

其中 ipaddress 即为用户输入的 IP 地址,我们通过以下代码尝试爬取成功:

import requests
url = "https://www.ip138.com/iplookup.asp?ip="
header={'user-agent':'Mozilla/5.0'}
url_back = "&action=2"
r = requests.get(url+"202.204.80.112"+url_back, headers = header,timeout=7)
print(r.status_code)
r.encoding = r.apparent_encoding
print(r.text)

我们可以自行将代码转换为爬虫通用框架。
下一篇内容:Python爬虫初级(三)—— Beautiful Soup 库入门

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐