【icourse163】学习python爬虫的代码整理

学习python爬虫的代码整理

代码思路等均参考课程:http://www.icourse163.org/learn/BIT-1001870001?tid=1002236011
1 对于一般的网页的抓取
def getHTML(url):
    try:
        response = requests.get(url,timeout = 30)
        response.raise_for_status() #如果状态不是200,则返回“产生异常”
        response.encoding = response.apparent_encoding
        return response.text
    except:
        return "产生异常"

if __name__=="__main__":
        url = "https://item.jd.com/5181380.html"
        print(getHTML(url))
2 亚马逊的例子
由于亚马逊有来源审查功能,因此限制User-Agent字段为Python的访问,所以采用以下方法爬取亚马逊商品:
def getHTMLAmazon(url):
    try:
        kv = {'User-Agent':'Mozilla/5.0'}
        response = requests.get(url, headers = kv)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return response.text
    except:
        return "爬取失败"   


if __name__=="__main__":
        url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
        print(getHTMLAmazon(url))
3 提供百度搜索的接口
def getBaiduso(keyword):
    try:
        kv ={"wd":keyword}
        response = requests.get("http://www.baidu.com/s", params = kv)
        print response.request.url
        response.raise_for_status()
        return len(response.text)
    except:
        return "爬取失败"

keyword = "Python"
print getBaiduso(keyword)
4 爬取图片并保存
def getPic(url,root):
    path = root +url.split('/')[-1]
    try:
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path):
            response = requests.get(url)
            response.raise_for_status()
            with open(path,"wb") as f:
                f.write(response.content)
                f.close()
                print "文件已保存"
        else:
            print "文件已存在"
    except:
        print "爬取失败"

url = "http://image.nationalgeographic.com.cn/2017/0526/20170526025441983.jpg"
root = "E://Python dir//"
getPic(url,root)
5 查询IP地址归属地
def getIP(ipaddress):
    try:
        kv ={"ip":ipaddress}
        response = requests.get("http://m.ip138.com/ip.asp", params = kv)
        print response.request.url
        response.raise_for_status()
        return response.text[-500:]
    except:
        return "爬取失败"

ipaddress = "202.204.80.112"
print getIP(ipaddress)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值