<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scourgen &#187; Python</title>
	<atom:link href="http://www.scourgen.com/category/python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.scourgen.com</link>
	<description></description>
	<lastBuildDate>Sat, 09 Apr 2011 15:28:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>用Python脚本抓取Linkedin中的公司信息</title>
		<link>http://www.scourgen.com/use_python_to_get_company_infomation_in_linkedin</link>
		<comments>http://www.scourgen.com/use_python_to_get_company_infomation_in_linkedin#comments</comments>
		<pubDate>Thu, 21 Jan 2010 18:05:29 +0000</pubDate>
		<dc:creator>scourgen</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.scourgen.com/?p=104</guid>
		<description><![CDATA[Linkedin是一个在国外比较知名的商务型SNS.最近在研究Linkedin时,发现Linkedin在添加个人信息时的公司名称提示功能很强大,通过这个功能几乎可以获取一份大型公司名录.于是用Python花了几分钟写了一个小脚本,顺利的获取到了6000余条公司信息. 代码如下: MySQL数据库结构: 关键字设置的很简单,从aa循环到zz,每个关键字能够搜到10条信息,所以不考虑数据重复性的话,这个程序一共能够抓取到26*26*10=6760条公司信息.而实际运行结果则是插入了6518条,而其中34条是重复数据,可见Linkedin的公司数量非常之多. 需要注意的几个问题: Linkedin公司介绍中有西欧字符,默认情况下无法插入sql,报错 解决办法是将sql字符串转换成utf8,同时也将数据库的连接属性改成utf8即可 公司简介中有些数据含有引号,导致sql语句构造失败]]></description>
			<content:encoded><![CDATA[<p>Linkedin是一个在国外比较知名的商务型SNS.最近在研究Linkedin时,发现Linkedin在添加个人信息时的公司名称提示功能很强大,通过这个功能几乎可以获取一份大型公司名录.于是用Python花了几分钟写了一个小脚本,顺利的获取到了6000余条公司信息.</p>
<p>代码如下:</p>
<pre class="brush: python; title: ; notranslate">import urllib2
import json
import MySQLdb

db = MySQLdb.connect(host=&quot;localhost&quot;,unix_socket='/Applications/MAMP/tmp/mysql/mysql.sock',port=3306,user=&quot;root&quot;,passwd=&quot;000000&quot;,db=&quot;linkedin_spider&quot;)
db.set_character_set(&quot;utf8&quot;)

def getData(str):
 req = urllib2.Request(
 url = 'http://www.linkedin.com/typeaheadv3/company?query='+str+'&amp;loc=P'
 )
 result = urllib2.urlopen(req).read()
 b= json.JSONDecoder().decode(result)
 for k in b[&quot;resultList&quot;]:

 cursor = db.cursor()
 if k.has_key(&quot;imageUrl&quot;):
 sql = u&quot;insert into companys values(null,\&quot;&quot;+k['displayName'].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;&quot;+k['subLine'].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;&quot;+k[&quot;headLine&quot;].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;&quot;+k['imageUrl']+&quot;\&quot;,\&quot;&quot;+k['url']+&quot;\&quot;,\&quot;&quot;+k['id']+&quot;\&quot;)&quot;
 else:
 sql = u&quot;insert into companys values(null,\&quot;&quot;+k['displayName'].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;&quot;+k['subLine'].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;&quot;+k[&quot;headLine&quot;].replace('&quot;','\\&quot;')+&quot;\&quot;,\&quot;null\&quot;,\&quot;&quot;+k['url']+&quot;\&quot;,\&quot;&quot;+k['id']+&quot;\&quot;)&quot;
 print sql
 cursor.execute(unicode(sql))
 print k['displayName']

a=&quot;abcdefghijklmnopqrstuvwxyz&quot;
i=0
for x in a:
for y in a:
 print i
 i=i+1
 getData(x+y)
</pre>
<p>MySQL数据库结构:</p>
<pre class="brush: sql; title: ; notranslate">SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `companys`
-- ----------------------------
DROP TABLE IF EXISTS `companys`;
CREATE TABLE `companys` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `displayName` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 `subLine` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 `headLine` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 `imageUrl` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 `url` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 `link_id` varchar(500) COLLATE utf8_bin DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;</pre>
<p>关键字设置的很简单,从aa循环到zz,每个关键字能够搜到10条信息,所以不考虑数据重复性的话,这个程序一共能够抓取到26*26*10=6760条公司信息.而实际运行结果则是插入了6518条,而其中34条是重复数据,可见Linkedin的公司数量非常之多.</p>
<p>需要注意的几个问题:</p>
<ul>
<li>Linkedin公司介绍中有西欧字符,默认情况下无法插入sql,报错
<pre class="brush: plain; title: ; notranslate">Traceback (most recent call last):
 File &quot;main.py&quot;, line 29, in &lt;module&gt;
 getData(x+y)
 File &quot;main.py&quot;, line 21, in getData
 cursor.execute(sql)
 File &quot;/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/cursors.py&quot;, line 149, in execute
 query = query.encode(charset)
&lt;span style=&quot;color: #ff0000;&quot;&gt;UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0142' in position 89: ordinal not in range(256)&lt;/span&gt;
smbp:Linkedin_Spider <span class='wp_keywordlink'><a href="http://www.scourgen.com" title="scourgen">scourgen</a></span>$ python2.6 main.py
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
 from sets import ImmutableSet</pre>
<p>解决办法是将sql字符串转换成utf8,同时也将数据库的连接属性改成utf8即可</p>
<pre class="brush: python; title: ; notranslate">
#设置连接属性:
db.set_character_set(&quot;utf8&quot;)
#u&quot;&quot; 代表将字符串转成unicode
sql=u&quot;insert ...&quot;
#再转一次,以防万一
cursor.execute(unicode(sql))
</pre>
</li>
<li>公司简介中有些数据含有引号,导致sql语句构造失败
<pre class="brush: python; title: ; notranslate">replace('&quot;','\\&quot;')可以将&quot;替换成\&quot;,以便构造sql语句
sql = &quot;insert ....&quot;+k['subLine'].replace('&quot;','\\&quot;')</pre>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.scourgen.com/use_python_to_get_company_infomation_in_linkedin/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

