python爬虫:案例一:360指数

pip installbeautifulsoup4
pip install requests
pip install selenium
下载  phantomjs(phantoms是一个无界面浏览器,用来解析js代码)
给 firefox 安装 firebug

创建一个目录名为baidupc
cd baidupc
创建虚拟环境
virtualenv macp

激活虚拟环境
进入macp/Scripts下输入命令
activate

mac下进入/macp/bin
source  activate

虚拟环境的好处在于环境独立,可以随便折腾也不影响自己原有的环境

将phantomjs-2.1.1-macosx.zip解压,把bin目录下的 phantoms拷贝到 baidupc/macp/bin下面
( phantomjs根据不同系统下载不同压缩包,windows下虚拟环境的目录应该是baiducp\macp\Script )


案例1:360指数
360指数的数据显示很直观,你在首页输入一个关键字再看一下url就知道了
(其实真实的url为: http://index.so.com/#trend?q=%E6%AC%A2%E4%B9%90%E9%A2%82 ,后面的中文要编码)
加上日期查询的url有三种:

了解到这些信息,我们再从html中找到需要的数据节点就知道如何获取基本数据了

#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import urllib
from selenium import webdriver

class Qh():
    def pc(seif,name,date='7'):
       #时间默认7天,参数’30’为三十天,’201603|201605’为自定时月份
        url_name=urllib.quote(name)
        #urllib.quote( )将中文url编码
        url='http://index.so.com/#trend?q='+url_name+'&t='+date
        driver = webdriver.PhantomJS() 
        #webdriver.PhantomJS() 调用PhantomJS浏览器 
        driver.get(url)
        sszs=driver.find_element_by_xpath('//*[@id="bd_overview"]/div[2]/table/tbody/tr/td[1]').text
        sszshb=driver.find_element_by_xpath('//*[@id="bd_overview"]/div[2]/table/tbody/tr/td[2]').text
        sszstb=driver.find_element_by_xpath('//*[@id="bd_overview"]/div[2]/table/tbody/tr/td[3]').text
       #搜索指数,搜索指数环比,搜索指数同比(均为全国数据)
        driver.quit
       #quit( )关闭
        return sszs+'|'+sszshb+'|'+sszstb
        

s=Qh()
print s.pc('欢乐颂')
print s.pc('欢乐颂','30')
print s.pc('欢乐颂','201603|201605')


结果:
1,392,286|36.28%|>1000%
657,310|>1000%|>1000%
657,310|>1000%|>1000%
(这里有个很有意思的地方,网上显示的201603|201605的数据与7天数据一样,而我爬下来的数据201603|201605与30天一样,当然按常理的确是应该与30天数据一致,但网页的显示不知道为什么和7天一致)

以上是最简单的版本,观察360指数的页面会发现数据可以选择地区,默认是全国,以上的程序爬取到的只是全国的数据,想要得到各地的数据,我们需要分析一下
打开  firefox 的 firebug 点击’更改’,选择’浙江',会发现这是一段ajax,我们在firebug中找XHR,会找到一个GET的地址,我们将url复制到浏览器中会得到一段JSON,这就是AJAX返回的数据
返回的json数据:
{"status":0,"data":[{"query":"\u6b22\u4e50\u9882","data":{"week_year_ratio":">1000%","month_year_ratio":">1000%","week_chain_ratio":"31.52%","month_chain_ratio":">1000%","week_index":97521,"month_index":47646}}],"msg":false}


我们会发现只有七天和一个月的 搜索指数,搜索指数环比,搜索指数同比

代码:
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import urllib
from selenium import webdriver

class Qh():
    def pc(seif,name,dq='全国'):
        url_name=urllib.quote(name)
        dq_name=urllib.quote(dq)
        url='http://index.so.com/index.php?a=overviewJson&q='+url_name+'&area='+dq_name
        driver = webdriver.PhantomJS() 
        driver.get(url)
        json=driver.find_element_by_xpath('/html/body/pre').text
        driver.quit
        return json

s=Qh()
print s.pc('欢乐颂')
print s.pc('欢乐颂','浙江')


结果:
{"status":0,"data":[{"query":"\u6b22\u4e50\u9882","data":{"week_year_ratio":">1000%","month_year_ratio":">1000%","week_chain_ratio":"36.28%","month_chain_ratio":">1000%","week_index":1392286,"month_index":657310}}],"msg":false}
{"status":0,"data":[{"query":"\u6b22\u4e50\u9882","data":{"week_year_ratio":">1000%","month_year_ratio":">1000%”,"week_chain_ratio":"31.52%","month_chain_ratio":">1000%","week_index":97521,"month_index":47646}}],"msg":false}



week_year_ratio为7天搜索指数同比
month_year_ratio为30天搜索指数同比
week_chain_ratio 为7天搜索指数环比
month_chain_ratio为30天搜索指数环比
week_index为7天搜索指数
month_index为30天搜索指数

如果要爬一堆关键字的话可以写一个配置文件,然后循环去抓取数据
如果只想要自己想要的数据而不是json 的话可以解析json,再存储到数据库或者文件中

如上方法,我们找一下搜索趋势图的json数据在一个URL中
http://index.so.com/index.php?a=soIndexJson&q=欢乐颂&area=全国

媒体关注度的json数据在另一个url请求中返回
http://index.so.com/index.php?a=soMediaJson&q=欢乐颂

于是我们改一下代码:
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import urllib
import requests
from selenium import webdriver

class Qh():
    def pc(seif,name,dq='全国'):
    	json_list=[]
        url_name=urllib.quote(name)
        dq_name=urllib.quote(dq)
        urls=['http://index.so.com/index.php?a=overviewJson&q='+url_name+'&area='+dq_name,"http://index.so.com/index.php?a=soIndexJson&q="+url_name+"&area="+dq_name,"http://index.so.com/index.php?a=soMediaJson&q="+url_name]
        #第一个url是指数概况返回的json数据,第二个url是搜索指数趋势返回的json数据,第三个url是媒体关注度返回的json数据
        for url in urls:
        	json=requests.get(url)
        	json_list.append(json.text)
        return json_list

s=Qh()
print s.pc('欢乐颂')
#print s.pc('欢乐颂','浙江')

结果:

[u'{"status":0,"data":[{"query":"\\u6b22\\u4e50\\u9882","data":{"week_year_ratio":">1000%","month_year_ratio":">1000%","week_chain_ratio":"-55.21%","month_chain_ratio":"928.29%","week_index":418452,"month_index":980627}}],"msg":false}', u'{"status":0,"data":{"index":{"\\u6b22\\u4e50\\u9882":"280|247|345|440|323|393|328|269|273|246|296|388|248|532|400|312|429|429|691|564|442|493|585|411|386|596|322|568|391|390|305|275|328|346|234|332|455|270|401|429|455|614|325|577|670|315|307|222|298|355|253|272|290|363|305|341|345|310|368|96|363|305|341|327|365|406|185|179|273|352|327|365|406|222|355|335|334|383|380|508|287|458|484|564|445|452|362|430|365|350|538|383|516|469|348|474|358|421|377|490|384|284|453|314|424|450|396|235|428|379|238|266|187|294|376|360|470|333|395|448|417|335|432|291|374|393|403|717|593|504|428|494|351|290|427|397|558|776|879|768|708|933|837|763|666|523|686|660|666|1557|2098|1672|1566|1211|946|890|1010|758|820|779|786|865|824|718|674|677|629|629|728|743|941|779|468|609|738|765|931|931|675|788|989|1015|922|1029|1000|763|720|717|725|716|749|772|655|705|690|480|681|651|636|497|354|432|707|1006|880|668|600|439|422|463|427|599|540|535|460|409|592|572|587|626|495|446|563|586|439|569|579|717|418|581|528|497|604|526|445|410|425|515|628|771|666|407|612|616|573|681|633|690|607|753|598|871|639|726|594|660|561|703|624|698|718|588|634|680|687|643|587|770|593|594|481|429|528|717|757|573|537|604|579|715|754|861|788|541|725|694|642|660|788|777|637|599|812|796|754|827|746|628|913|1483|786|720|1038|797|680|859|791|739|727|1058|898|888|839|855|957|832|987|1045|847|831|864|1009|1013|1124|891|868|1020|967|1036|996|1162|889|946|963|1036|1139|1117|1346|1191|955|1133|1080|1063|971|1188|1054|1005|1406|1370|1063|971|1188|1054|681|589|648|662|654|823|766|680|580|640|627|704|585|661|612|584|696|693|651|804|757|720|730|744|625|740|532|469|696|570|653|686|559|553|523|636|457|561|583|486|444|467|508|531|492|530|576|543|294|505|396|404|455|510|576|613|603|521|490|599|627|647|477|565|541|560|441|687|759|642|587|652|597|515|625|692|763|626|520|558|617|565|738|708|604|591|522|691|614|631|693|415|572|653|569|524|641|641|621|590|540|597|634|740|711|543|578|671|707|553|846|769|532|578|870|586|559|708|832|631|722|490|720|687|685|633|704|914|921|861|911|1058|877|778|881|942|852|839|975|867|780|930|1059|869|958|992|863|1062|923|956|1029|724|867|848|726|705|649|688|715|866|811|586|596|717|581|656|734|743|530|527|578|540|493|595|509|380|449|403|381|523|528|477|381|371|346|350|329|515|456|496|428|460|385|401|442|435|444|418|428|382|429|361|543|343|452|411|413|355|457|464|351|349|275|378|365|420|430|392|363|369|395|481|338|454|351|410|433|440|502|442|344|377|328|392|392|438|400|396|337|361|428|370|452|381|378|336|348|427|395|450|499|489|494|371|322|412|455|522|470|427|413|445|439|472|468|374|369|465|484|420|454|679|511|442|442|587|630|467|535|540|448|573|452|483|454|477|644|518|538|562|588|583|648|601|516|610|628|489|593|701|603|523|579|546|514|620|585|726|586|555|598|606|674|743|695|597|620|682|537|671|810|721|690|685|731|702|584|812|639|655|679|668|675|609|700|810|797|774|741|832|648|561|667|536|608|743|700|764|924|785|765|831|901|819|862|886|822|1017|1019|1180|1001|745|942|843|896|1037|782|659|615|686|619|677|629|810|681|646|752|744|674|619|707|563|562|563|636|516|548|479|620|572|684|583|547|684|633|672|662|687|675|629|723|681|609|831|815|817|832|671|706|799|750|735|832|750|861|644|680|603|694|591|680|575|658|568|576|666|646|606|594|549|624|486|655|604|471|435|509|609|660|751|648|556|589|580|536|653|657|631|419|433|578|575|573|897|701|616|526|614|510|600|689|703|614|565|492|568|612|744|674|538|536|658|756|545|721|782|656|569|581|720|627|780|681|470|525|518|604|691|689|644|503|504|512|529|503|548|631|492|489|573|499|507|581|646|413|475|638|620|563|615|762|607|504|510|493|534|689|539|437|462|487|476|525|630|553|506|630|489|596|500|558|587|462|428|472|389|437|485|648|461|411|381|411|459|359|413|293|371|439|386|427|458|460|403|400|506|413|425|395|478|386|401|329|373|406|394|479|399|411|359|451|402|423|468|326|387|380|309|426|369|357|278|330|412|341|381|418|376|362|329|342|366|375|471|437|321|337|688|415|299|427|439|344|363|401|617|529|465|407|411|346|334|402|406|423|328|399|513|395|376|472|582|566|981|921|685|880|916|956|647|838|673|732|743|1049|838|736|561|649|846|675|1237|1170|935|1148|1197|1244|710|812|1059|810|741|2521|1367|1285|2087|2140|1266|1394|3407|2343|1378|1429|1235|1090|1160|1018|1093|1202|1055|1208|1043|1092|1211|1154|1150|1213|1319|1935|2171|3912|2992|2791|1796|1388|1387|1577|1635|1647|1708|1546|1591|1253|2435|1631|1575|1529|1220|1321|1137|1356|1393|1149|1293|981|1171|1051|1242|1311|1182|1249|1235|902|1177|1071|1033|900|1096|1031|1153|1156|1250|1356|1454|1527|1264|1376|1097|1165|1278|1170|1016|625|924|1006|864|863|829|760|682|749|781|769|667|1051|926|882|653|880|795|759|758|827|658|830|810|828|796|880|744|819|699|667|625|719|685|1337|3307|1888|1160|1019|917|983|822|1139|1348|800|897|905|863|711|642|748|729|728|671|1014|692|560|1113|661|654|751|6084|5077|2468|2058|1924|1445|1480|1774|1483|1355|1313|1165|776|1215|997|1160|1093|1763|1924|1596|1932|2327|1822|2202|2032|1663|1495|1860|1684|1882|5133|3520|3084|2531|2571|2876|6814|16093|8639|5317|7677|14702|13027|14366|15037|16506|24462|60131|281052|426452|599216|665499|653403|729027|870499|994778|1056861|1196564|1214033|1089946|1116086|1188689|1350368|1411547|1506474|1642194|1530642|1535712|1515791|1520920|1435334|844694|796353|746529|670934|525677|474253|446947|419482|419293|436797|406751|325642"},"period":{"from":"2013-01-11","to":"2016-05-23"}},"msg":false}', u'{"status":0,"data":{"media":{"\\u6b22\\u4e50\\u9882":"28|9|13|138|25|26|22|17|13|16|98|68|57|48|55|9|10|25|14|29|53|26|9|27|37|54|25|14|34|9|8|2|22|7|5|24|20|17|36|16|29|55|8|22|32|29|11|27|14|12|9|3|21|13|33|20|33|19|5|27|40|25|22|18|23|8|43|30|26|16|119|6|3|14|23|16|19|22|6|7|24|16|11|7|79|11|19|23|31|17|16|40|3|20|25|13|20|21|68|17|8|32|33|25|35|60|33|50|22|6|15|43|23|4|15|27|54|25|41|43|9|9|39|102|27|30|25|7|7|38|45|14|42|41|13|2|27|27|18|182|57|29|8|54|72|52|28|25|60|42|24|8|12|26|202|20|25|48|91|30|27|20|5|6|5|17|11|41|39|10|9|71|27|11|23|37|26|7|20|20|48|35|80|18|6|34|39|161|56|58|15|12|26|218|55|38|110|16|30|51|54|42|79|23|24|24|27|92|38|50|43|57|36|59|45|53|45|32|7|14|37|39|34|43|19|17|6|48|48|18|43|188|39|37|51|29|99|38|32|22|20|33|32|59|44|94|21|24|92|60|47|19|25|16|35|30|36|41|51|39|32|29|47|13|16|25|8|11|17|16|29|15|33|30|17|7|24|17|39|32|31|15|64|40|33|36|11|32|6|85|86|48|18|33|70|25|224|74|42|57|52|66|19|8|24|30|51|42|48|12|14|28|21|72|22|26|8|8|27|19|46|48|34|27|10|26|34|52|77|52|38|38|61|49|62|117|310|72|23|57|66|45|55|159|108|91|99|109|109|42|40|22|10|19|114|73|73|106|11|12|127|188|130|88|80|30|20|250|134|65|86|95|37|19|46|129|78|255|160|34|49|33|154|186|56|74|12|9|37|6|16|21|38|148|14|96|65|128|80|37|18|30|12|22|35|43|32|33|43|109|239|275|150|112|1|7|13|73|27|73|37|12|0|28|44|88|81|91|39|19|38|50|150|212|62|15|24|126|53|40|25|43|17|14|20|23|59|112|61|26|0|2|43|23|76|74|15|10|110|123|48|118|53|12|19|106|133|34|104|50|6|34|38|178|65|22|20|12|73|55|59|89|370|85|34|20|31|54|33|69|53|11|0|98|114|299|2458|108|32|10|52|21|40|105|157|38|30|30|55|90|50|37|25|0|200|30|21|40|70|12|32|50|51|16|63|43|5|9|52|21|27|35|30|13|52|179|73|351|126|137|22|12|273|142|41|113|85|11|19|81|76|25|17|28|24|43|55|79|357|81|168|40|98|54|23|332|104|46|10|0|37|20|97|36|53|9|7|70|87|25|49|65|28|11|66|44|22|65|102|9|29|122|60|32|67|21|19|21|106|114|77|78|101|17|9|3|45|69|54|37|11|3|33|73|21|101|46|5|14|54|109|82|44|138|10|83|38|49|7|4|10|10|7|26|12|27|29|69|16|4|127|80|48|19|31|19|2|64|24|107|61|44|55|15|30|30|46|69|69|43|49|46|45|57|97|60|15|14|191|89|25|92|61|3|4|66|96|34|24|62|16|17|51|28|26|35|115|6|10|42|40|37|53|106|7|29|51|22|29|53|77|6|4|164|35|44|33|39|9|57|61|57|48|57|73|5|6|51|62|111|35|21|8|25|92|34|125|25|14|8|2|16|21|26|44|62|8|6|52|46|29|78|59|8|3|20|46|32|47|21|10|122|72|67|39|51|79|9|2|75|14|33|10|42|20|27|49|30|12|8|14|5|5|8|2|38|32|20|16|7|17|13|25|32|18|9|8|26|31|16|13|13|28|1|22|18|40|15|18|1|19|21|5|32|25|10|5|15|13|23|101|23|13|17|3|2|38|42|72|60|60|75|144|39|27|45|20|1|8|41|36|84|58|33|10|15|39|195|193|86|15|13|17|130|27|29|51|46|40|6|18|48|13|22|41|48|56|59|26|28|32|27|12|9|35|40|20|103|54|16|13|71|120|46|66|43|25|9|17|28|41|26|13|13|12|32|20|16|31|37|8|4|3|28|21|22|33|6|5|12|25|9|32|7|3|2|8|11|17|49|23|2|20|30|22|23|40|68|15|1|19|51|76|30|31|3|21|17|16|98|48|15|2|3|7|8|16|93|46|2|5|25|28|15|12|46|42|39|18|19|54|19|29|33|51|85|144|65|40|39|47|41|42|42|59|25|35|56|90|45|12|19|40|29|64|22|59|30|17|82|37|21|0|0|0|9|39|38|22|3|25|132|85|14|4|4|19|6|4|6|7|55|89|29|76|45|9|122|381|218|275|469|359|263|300|402|266|277|305|301|294|356|312|327|302|308|550|464|439|421|334|272|389|592|597|614|566|447|357|536|821|592|733|673|503|492|410|442|408|457|326|293|322|359|411|384|469|345|301|352|345|376|298|440|408|271|254|273|236|244|256|262|227|226|424|241|258|217|354|168|258|255|278|277|205|254|253|204|281|214|266|192|176|141|133|353|258|373|297|352|190|139|184|238|247|166|240|332|208|288|155|304|220|140|49|64|78|78|84|81|458|149|52|94|43|33|50|110|48|126|213|199|159|81|117|48|40|495|152|193|125|87|73|53|70|109|194|176|121|77|57|128|134|333|339|137|65|66|110|78|145|129|108|175|322|146|390|313|219|536|336|170|130|200|165|325|301|428|354|298|209|269|273|1311|717|375|490|1441|1974|1361|1112|909|695|1500|1714|2394|1904|2259|1428|1676|3373|9185|3358|3754|4517|2481|2248|2131|4436|4609|5701|5562|4332|3501|6080|8098|8192|7198|7153|4529|4652|4998|4550|3964|4132|7670|3982|3395|4514"},"period":{"from":"2013-01-11","to":"2016-05-23"}},"msg":false}']


数据量很大,数据也很全,摆脱了selenium和PhantomJS,你会发现速度非常的快,飞一般的感觉

不知道是360没钱担心服务器受不了过多请求还是怎么,一次性把所有数据返回过来,很省事儿,不想百度那么恶心

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值