Python网络爬虫学习(二)

十五. 京东商品页面的爬取

import requests
r = requests.get("https://item.jd.com/3668211.html")
r.status_code

r.encoding

r.text[:1000]

十六. 亚马逊商品页面的爬取

import requests

def main():
    url = "https://www.amazon.cn/dp/B01M8L5Z3Y/ref=sr_1_1?ie=UTF8&qid=1533928429&sr=8-1&keywords=%E6%9E%81%E7%AE%80"
    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("爬取失败")

if __name__ == "__main__":
    main()

十七. 百度360搜索关键词提交

百度搜索关键词

# Baidu research code#
import requests

def main():
    keyword = "Python"
    try:
        kv = {'wd':keyword}
        r = requests.get("http://www.baidu.com/s",params = kv)
        print(r.request.url)
        r.raise_for_status()
        print(len(r.text))
    except:
        print("爬取失败")

if __name__ == "__main__":
    main()

360搜索代码

#360 research code#
import requests

def main():
    keyword = "Python"
    try:
        kv = {'q':keyword}
        r = requests.get("http://www.so.com/s",params = kv)
        print(r.request.url)
        r.raise_for_status()
        print(len(r.text))
    except:
        print("爬取失败")

if __name__ == "__main__":
    main()

十八. 网络图片的爬取和存储

import requests
import os

def main():
    url = "http://imgsrc.baidu.com/forum/pic/item/aa64034f78f0f736a4a890f80955b319eac413f0.jpg"
    root = "D://pics//"
    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("爬取失败")

if __name__ == "__main__":
    main()

十九. IP地址归属地的自动查询

import requests

def main():
    url = "http://m.ip138.com/ip.asp?ip="
    
    try:
        r = requests.get(url+ '202.204.80.112')
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print(r.text[-500:])
        
    except:
        print("爬取失败")

if __name__ == "__main__":
    main()

二十。单元小结

二十一. 第二周内容导学

二十二. beautiful soup库的安装

 

阅读更多
换一批

没有更多推荐了,返回首页