CS109 Lecture 7

CS109 Lecture 7

Data Scraping

Sources
  • From a Web Sites
  • With An API
Copyrights and permission
  • Be careful and polite
  • Give credit
  • Care about media law
  • Don’t be evil
Useful tags
<h1></h1>
<p></p>
<br>
<a href = 'url'>Link</a>

Useful Libraries for Scraping

  • urllib
  • beautifulsoup
  • pattern
  • LXML

Get Data From Website

url = 'url'
scource = urllib2.urlopen(url).read()
soup = bs4.BeautifulSoup(source)
soup.findAll('a') # find <a><\a> tag
tag = soup.find('a')
tag.get('href')
C = soup.findAll('p',{'class':'Event'})
t=C[0] 
t.findNextSiblings

Get Data With An API

import json # JavaScript Obejct Notation
import requests
api_key = 'mykey'
url = 'url' + api_key
scource = urllib2.urlopen(url).read()
#---simple example--------
a = {'a':1,'b':2}
s = json.dump(a) 
a2 = json.loads(s) 
#-------------------------
dataDict = json.loads(data)
dtatDict.keys()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
补全以下代码private String cid;// Course id, e.g., CS110. private String name;// Course name, e.g., Introduce to Java Programming. private Integer credit;// Credit of this course private GradingSchema gradingSchema; //Grading schema of this course // enum GradingSchema{FIVE_LEVEL, PASS_FAIL} private Integer capacity;// Course capacity. private Integer leftCapacity;// Course capacity left. You should update the left capacity when enrolling students. private Set<Timeslot> timeslots;// One course may have one or more timeslots. e.g., a lecture in Monday's 10:20-12:10, and a lab in Tuesday's 14:00-15:50. public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) // constructor public void addTimeslot(Timeslot timeslot) //Record a timeslot for this course private Integer id;// A unique student id, should be an 8-digit integer: Undergraduates' ids should start with 1; Postgraduates' ids should start with 3. e.g., 12213199. private String name;// Student’s name private Map<Course, Grade> courses;// Enrolled courses, using Map structure to store course and its grade as a pair. Grade is an enum type enum Grade{PASS,FAIL,A,B,C,D,F}with an attribute: Double gradePoint protected Student(Integer id, String name) // constructor public abstract boolean canGraduate() // Checks if this student satisfies all the graduating conditions. Hint: you are allowed to change this abstract method into non-abstract to check if the student satisfies the common graduation conditions. public void enroll(Course course) // Tries to enroll the course, do some checks before enrolling. public void recordGrade(Course course, Grade grade)// Records the grade of a course that is current learning. public double getGpa() // Calculates the GPA for this student. public UndergraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for undergraduate students public PostgraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for postgraduate students
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值