1.抓取教务处主界面,存于一个txt文档中.
import requests
file_path = r"E:\教务处.txt"
try:kv = {'user-agent':'Mozilla/5.0'}r = requests.get("http://jwch.sdut.edu.cn/", headers=kv)r.raise_for_status()r.encoding = r.apparent_encodingwith open(file_path, 'w') as file_obj:file_obj.write(r.text)
except:print("爬取失败")

2.百度搜索关键字。

import requests
try:kv = {'wd':'Python'}r = requests.get("http://www.baidu.com/s", params=kv)#百度的关键词接口:http://www.baidu.com/s?wd=keywordprint(r.request.url)r.raise_for_status()print(len(r.text))
except:print("爬取失败")

3.抓取图片。

import requests
import osurl = "http://img1001.pocoimg.cn/image/poco/works/36/2018/0307/21/15204284272111499_46378737_H1920.jpg"
root = 'E://pics//'
image_path = root + url.split('/')[-1]
try:r = requests.get(url)r.raise_for_status()if not os.path.exists(root):os.mkdir(root)if not os.path.exists(image_path):r = requests.get(url)with open(image_path, 'wb') as file_obj:file_obj.write(r.content)print('图片保留成功')
except:print("爬取失败")

4. ip138 爬取

import requestsurl = "http://m.ip138.com/ip.asp?ip="#ip138 查询接口
ip = '202.204.80.112'
try:r = requests.get(url + ip)r.raise_for_status()r.encoding= r.apparent_encodingprint(r.text[-500:])print("爬取成功!")
except:print("爬取失败!")

5.抓取中国大学排名

from bs4 import BeautifulSoup
import requests
import bs4
kv = {"user-agent":"Mozilla/5.0"}
def getHTMLText(url):try:r = requests.get(url, headers = kv, timeout = 30)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.textexcept:print("getHTMLText fail")return ""def fillUnivList(ulist, html):soup = BeautifulSoup(html, "html.parser")for tr in soup.find('tbody').children:if isinstance(tr, bs4.element.Tag):tds = tr('td')ulist.append([tds[0].string, tds[1].string, tds[3].string])def printUnivList(ulist, num):prmod = "{0:^10}\t {1:{3}^10}\t {2:{3}^10}\t"print(prmod.format("排名","学校", "总分", chr(12288)))for i in range(num):print(prmod.format(ulist[i][0],ulist[i][1], ulist[i][2], chr(12288)))
def main():uinfo = []url = "http://www.zuihaodaxue.com/zuihaodaxuepaiming2016.html"html = getHTMLText(url)fillUnivList(uinfo, html)printUnivList(uinfo, 20)
main()

6. 淘宝抓取商品信息

import re
import requestsdef getHtml(url):try:kv = {"ueser-agent":"Mozalli/5.0"}r = requests.get(url, timeout = 30, headers = kv)r.encoding = r.apparent_encodingr.raise_for_status()return r.textexcept:print("getHtml faild.")return ""def parserHtml(html, good_list):regename = r'"raw_title":".*?"'regexprice = r'"view_price":"[\d.]*"'regexn = re.compile(regename)regexp = re.compile(regexprice)names = regexn.findall(html)prices = regexp.findall(html)for i in range(len(names)):name = eval(names[i].split(":")[1])price = eval(prices[i].split(":")[1])good_list.append([name, price])def display(good_list):print_mode = "{0:{3}<4}\t{1:{3}<16}\t {2:{3}<8}\t"cnt = 1for i in range(len(good_list)):print(print_mode.format(cnt, good_list[i][1], good_list[i][0], chr(12288)))cnt += 1def main():name = input("输入货物名:")raw_url = "https://s.taobao.com/search?q=" + namebase = 44num = input("输入查询深度:")num = int(num)cnt = 1good_list = []print_mode = "{0:{3}<4}\t{1:{3}<16}\t {2:{3}<8}\t"print(print_mode.format("序号", "价格", "商品名", chr(12288)))for i in range(num):try:html = getHtml(raw_url + '&s=' + str(num * i))parserHtml(html, good_list)except:continuegood_list.sort(key = lambda a: float(a[1]))display(good_list)
main()

7.爬去股票信息

import re
import  requests
from bs4 import BeautifulSoup
urllist = "http://quote.eastmoney.com/stocklist.html"
urlbaidu = "https://gupiao.baidu.com/stock/"
def getHtml(url):kv = {"user-agent":"Mozilla/5.0"}try:r = requests.get(url, headers=kv)r.raise_for_status()r.encoding = r.apparent_encodingreturn  r.textexcept:return ''def getStockList():html = getHtml(urllist)soup = BeautifulSoup(html, "html.parser")tmp = soup.find('div', attrs={'class':'qox'})tagA = tmp.find_all('div', attrs={'class':'quotebody'})tagA = tmp.find_all('a')regex = r'[s][hz]\d{6}'regex = re.compile(regex)stockid = []for a in tagA:try:href = a.attrs['href']sid = regex.findall(href)[0]stockid.append(sid)except:continuereturn stockiddef getinfoDict():stockid = getStockList()stockList =[]for id in stockid:try:infoDict = {}url = urlbaidu + id + '.html'html = getHtml(url)if html == '':continuesoup = BeautifulSoup(html, 'html.parser')tables = soup.find('div', attrs={'class': 'stock-bets'})name = tables.find(attrs={'class': 'bets-name'}).text.split()[0]infoDict.update({"股票名称": name})print("股票名称:%s %s" % (infoDict["股票名称"], id))div = tables.find('div', attrs={'class': 'bets-content'})dts = div.find_all('dt')dds = div.find_all('dd')for i in range(len(dts)):print(dts[i].string + ':' + dds[i].string)infoDict[dts[i].string] = dds[i].stringstockList.append(infoDict)except:continuereturn stockListgetinfoDict()

首次使用requests库抓取代码相关推荐

  1. 利用requests库抓取猫眼电影排行

    文章目录 1.抓取目标 2.准备工作 3.抓取分析 4.抓取首页 5.正则提取 6.写入文件 7.整合代码 8.分页爬取 9.运行结果 10.本节代码 最近刚开始了解爬虫,学习了一下基本库的使用.跟着 ...

  2. python 利用requests库抓取网站图片

    截图放在下方: 我们来看下我们要的图片都在哪 框起来这些图就是我要的,数量多的不得了,看来这个网站积累了很久了,现在我们要用5分钟时间来拿到所有图片 接下来让我们看下源代码来解析一下这些图片的地址吧. ...

  3. 通过python requests第三方库抓取淘宝商品名称和信息价格

    项目名称:淘宝爬虫之抓取商品标题和价格信息 任务背景: 公司要求提取各电商平台的咖啡机的价格信息,在淘宝开放平台找不到合适的API..获取价格就是为了产品定价,和将来打价格战. 实现用到的库:requ ...

  4. 用bs4和requests库,抓取nga舰队Collection萌战玩家投票

    import requests from bs4 import BeautifulSoupurl = 'http://bbs.ngacn.cc/read.php?tid=13428951' req = ...

  5. 用python爬取qq空间内容_利用Fiddler抓包和py的requests库爬取QQ空间说说内容并写入文件...

    [Python] 纯文本查看 复制代码#!C:\Program Files\Python36 python # -*- coding: UTF-8 -*- """ @au ...

  6. python爬虫基础-requests库

    python爬虫基础-requests库 python爬虫 1.什么是爬虫? 通过编写程序,模拟浏览器上网,然后让其去互联网上抓取数据的过程. 注意:浏览器抓取的数据对应的页面是一个完整的页面. 为什 ...

  7. requests库的安装

    本文是基于 中国大学MOOC教程 中<Python网络爬虫与信息提取> 做的学习笔记,笔者在这里做一个分享 Request 库是python的第三方库,它也是目前公认的爬取网页最好的第三方 ...

  8. python accept解析_python中requests库使用方法详解

    一.什么是Requests Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤量的 ...

  9. python编写请求参数带文件_python requests 库请求带有文件参数的接口实例

    有些接口参数是一个文件格式,比如fiddler 抓包参数如下显示 这个接口的 form-data fiddler 显示的和不带文件参数的接口有明显区别,显示的不是简单的键值对,所以我们也不能只通过 d ...

最新文章

  1. 编写高质量JavaScript代码的基本技巧
  2. 用AI指挥另一个AI,GAN+CLIP的组合成了“CG艺术家”
  3. OpenCV之core 模块. 核心功能(1)Mat - 基本图像容器 OpenCV如何扫描图像、利用查找表和计时 矩阵的掩码操作 使用OpenCV对两幅图像求和(求混合(blending))
  4. AV1解码器dav1d性能提升100%
  5. javascript --- 创建一个二维数组
  6. Hibernate懒加载
  7. C++ Websites
  8. 一元三次方程重根判别式_一元四次方程的常规解法
  9. C语言的5种简单排序算法
  10. Python利用wakeonlan库 局域网 网络唤醒电脑
  11. [ JAVA ] 共有前缀
  12. 免费送5000多G之java,javaweb,python,大数据,区块链,安卓等的学习资源
  13. php 日期相减获得天数,PHP两个日期相减 计算天数、月、年[Stack Overflow]
  14. 利用FTP将Linux文件备份到Windows
  15. 我的PCB走线经验归纳
  16. MFC 修改字体的颜色
  17. ## 虚幻四引擎学习——初学者
  18. java jdom_java使用Jdom实现xml文件写入操作实例
  19. Android 游戏-超级玛丽(Android studio)
  20. 分类-3-生成学习-3-朴素贝叶斯模型、laplace平滑、多元伯努利事件模型、多项式事件模型

热门文章

  1. 中间人攻击(MITM)姿势总结
  2. android 7.0魅蓝,魅族魅蓝3S的手机系统是什么
  3. 【好用的工具】搭建个人博客网站(域名备案 + https免费证书)
  4. js 设置cookie一天内过期(包含24小时与一个自然日)
  5. Python爱心源码抖动图
  6. 方舟找不到mod服务器了,方舟生存进化灭绝MOD全图文攻略 方舟灭绝MOD上手指南_MOD介绍及服务器设置_游侠网...
  7. 【搜索】JZOJ_3319 LOJ_2686 「BalticOI 2013」雪地足迹 Tracks in the Snow
  8. 【RocketMQ】ubuntu18下部署RocketMQ集群
  9. Netty 专栏——Future-Listener机制与心跳机制
  10. OpenGL渲染模型 || 3. opengl 将模型成渲染图片