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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值