美文网首页
入门级爬虫(2)

入门级爬虫(2)

作者: yeshan333 | 来源:发表于2018-10-01 20:30 被阅读14次

requests库入门实操我的个人博客

  • 京东商品页面爬取
  • 亚马逊商品页面的爬取
  • 百度/360搜索关键字提交
  • IP地址归属地查询
  • 网络图片的爬取和储存

1.京东商品页面的爬取

华为nova3

import requests
def GetHTMLText(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text[:1000]
    except:
        print("爬取失败")
if __name__ == '__main__':
    url = "https://item.jd.com/30185690434.html"
    print(GetHTMLText(url))
image

2.亚马孙商品页面的爬取

某些网站可能有反爬机制。通常的反爬策略有:

  1. 通过Headers反爬虫
  2. 基于用户行为反爬虫
  3. 动态页面的反爬虫
    参考
#如网站对Headers的User-Agent进行检测,可定制请求头伪装成浏览器
import requests
def GetHTMLText(url):
    try:
        #定制请求头
        headers = {"user-agent":"Mozilla/5.0"}

        r = requests.get(url,headers = headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text[:1000]
    except:
        print("爬取失败")
if __name__ == '__main__':
    url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
    print(GetHTMLText(url))

3.百度/360搜索关键字提交

使用params参数,利用接口keyword

#百度搜索引擎关键词提交接口: http://www.baidu.com/s?wd=keyword
#360搜索引擎关键词提交接口: http://www.so.com/s?q=keyword

import requests


def Get(url):
    headers = {'user-agent':'Mozilla/5.0'}
    key_word = {'wd':'python'}
    try:
        r=requests.get(url,headers=headers,params=key_word)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print(r.request.url)
        #return r.request.url
        return r.text
    except:
        return "爬取失败"

if __name__ == '__main__':
    url = "http://www.baidu.com/s"
    #print(Get(url))
    print(len(Get(url)))
image

4.IP地址归属地查询

使用IP138的API接口
http://m.ip138.com/ip.asp?ip=ipaddress

# ip地址查询
import requests

url ="http://m.ip138.com/ip.asp?ip="
ip = str(input())
try:
    r= requests.get(url+ip)
    r.raise_for_status()
    print(r.status_code)
    #r.encoding = r.apparent_encoding
    print(r.text[-500:])
except:
    print("failed")
image

5.网络图片的爬取和储存

# spider_for_imgs

import requests
import os

url = "http://n.sinaimg.cn/sinacn12/w495h787/20180315/1923-fyscsmv9949374.jpg"
#C:\Users\Administrator\Desktop\spider\first week\imgs/
root = "C://Users/Administrator/Desktop/spider/first week/imgs/"

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("save successfully!")
    else:
        print("file already exist!")
except:
    print("spider fail")
image

image

相关文章

  • 入门级爬虫(2)

    requests库入门实操我的个人博客 京东商品页面爬取 亚马逊商品页面的爬取 百度/360搜索关键字提交 IP地...

  • 反虫与反爬虫二三事

    爬虫,反爬虫,反反爬虫……魔高一尺,道高一丈。无穷无尽的进化抗争史。 入门级反爬虫:验证一下Headers里面的一...

  • 入门级爬虫

    Requests库入门我的个人博客 requests Requests库的7个主要方法 Requests库的get...

  • 爬简书7日热门

    这一周忙成狗,终于在周六把这个入门级的小爬虫写粗来了。推荐这个专题:如何学习Python爬虫[入门篇]? - 知乎...

  • Python 爬虫爬坑路(二)——B站图片,咸鱼的正确 GET

    前言 昨天在写完 入门级爬虫之后 ,马上就迫不及待的着手开始写 B站的图片爬虫了,真的很喜欢这个破站呢 (〜 ̄△ ̄...

  • 入门级小说爬虫

    开发环境python: 3.7.2 第三方库:requests: 2.21.0lxml安装:pip install...

  • python-爬虫基础(慕课网)

    二.爬虫简介以及爬虫的技术价值 2-1:爬虫是什么? 2-2:爬虫技术的价值? 三.简单爬虫架构 3-1:简单爬虫...

  • 爬虫正传-江湖路远-0102-少侠师承何处

    少侠初入江湖,尚不知江湖险恶,入门级别的爬虫很快就被人识破,并对爬虫程序的发起IP地址进行了封锁WHY?因为少侠不...

  • Python爬虫入门级项目

    项目简介 由于最近调研文献需要,想查看KDD2017年的论文是否有相关的论文。但是KDD accept的论文有20...

  • 500 行 Python 代码构建一个轻量级爬虫框架

    引言 玩 Python 爬虫有段时间了,但是目前还是处于入门级别。xcrawler 则是利用周末时间构建的一个轻量...

网友评论

      本文标题:入门级爬虫(2)

      本文链接:https://www.haomeiwen.com/subject/slnnoftx.html