Requests库入门--实战

京东商品页面的爬取

【华为荣耀8】荣耀8 4GB+64GB 全网通4G手机 魅海蓝【行情 报价 价格 评测】-京东 (jd.com)

 

#!/usr/bin/env python
# -- coding: utf-8 --
# @Time : 2023/3/30 14:55
# @File : 京东商品页面的爬取.py
import requests
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
url="https://item.jd.com/2967929.html"
try:
    r=requests.get(url,headers=headers)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
    print(r.text[:1000])
except:
    print('失败')
print(r.status_code)
print(r.apparent_encoding)
print(r.text)

亚马逊商品页面的爬取

《极简:在你拥有的一切之下,发现你想要的生活》 【摘要 书评 试读】图书 (amazon.cn)

采用伪装头,否则会拒绝访问

 

#!/usr/bin/env python
# -- coding: utf-8 --
# @Time : 2023/3/30 14:55
# @File : 京东商品页面的爬取.py
import requests
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
url="https://www.amazon.cn/gp/product/B01M8L5Z3Y"
try:
    r=requests.get(url,headers=headers)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
    print(r.text[:1000])
except:
    print('失败')
print(r.status_code)
print(r.apparent_encoding)
print(r.text[:10000])

百度360搜索关键字提交

百度的关键词接口:keyword_百度搜索 (baidu.com)

360的关键词接口:keyword_360搜索 (so.com) 

就是替换keyword就能像搜索引擎提交关键词了 

#!/usr/bin/env python
# -- coding: utf-8 --
# @Time : 2023/3/30 14:55
# @File : 京东商品页面的爬取.py
import requests
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
url="http://www.baidu.com/s"
keyword="Python"
try:
    kv={'wd':keyword}
    r=requests.get(url,headers=headers,params=kv)
    print(r.request.url)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
    print(len(r.text))
except:
    print('失败')

网络图片的爬取和存储

tooopen_sl_151556155680624.jpg (300×198)

 在页面中通过审查元素,找到图片的地址,记住这个路径,我们要通过爬虫把这个图片保存到我们的D盘下

import requests
import os
# 保存的图片路径
root="D://pics//"
url="https://img08.tooopen.com/20190705/tooopen_sl_151556155680624.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("爬取失败")

 这段代码使用了Python的requests库来爬取一张图片,并将其保存到本地。具体步骤如下:

  1. 导入requests和os库。

  2. 定义保存图片的路径root和图片的url。

  3. 通过url.split("/")[-1]获取图片的文件名,并将其加入到路径中,得到path。

  4. 判断保存图片的文件夹是否存在,如果不存在则创建。

  5. 判断图片是否已经存在于本地,如果不存在则使用requests库的get方法获取图片内容,并将其写入本地文件中。

  6. 如果文件已经存在,则输出“文件已存在”。

  7. 如果出现异常,则输出“爬取失败”。

需要注意的是,这段代码中使用了try-except语句来捕获异常,以避免程序出现错误时直接退出。同时,在写入文件时要调用close()方法来关闭文件。

IP地址归属地的自动化查询

https://www.ip138.com/iplookup.php?ip=address

 

 

import requests
url="https://www.ip138.com/iplookup.php?ip="
# url="https://www.ip138.com/ip.asp?ip="
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
try:
    r=requests.get(url+'36.152.44.95',headers=headers)
    r.status_code=r.raise_for_status()
    print(r.text[-500:])
except:
    print('失败')

猜你喜欢

转载自blog.csdn.net/weixin_64612659/article/details/129857877