0.准备工作

 

1.相关教程

        Python 爬虫系列教程:http://cuiqingcai.com/1052.html
        Python Web课程:http://www.cnblogs.com/moonache/p/5110322.html
        Python 中文参考文档:http://python.usyiyi.cn/

2.说明

        下面的代码基本只处于可用阶段,欠缺移植性,本篇Bolg更多是一种记录
        本篇Bolg中使用的是Python2.7
        CPU信息从该网址获取:http://zj.zol.com.cn/

3.效果

        

1.获取CPU型号和主频信息

 

1.神伤的AJAX

        本来想直接爬, 结果发现http://zj.zol.com.cn/ 翻页后链接不变,通过chrome F12的控制台发现是通过AJAX刷新的
我只需修改page=n 即可获取第n页的CPU信息
 

2.获取CPU名字

格式举列
        tag.contents[0] :AMD \u7cfb\u5217 A8-7670\uff08\u76d2\u88c5\uff09<\/a>\r\n\t\t\t\t\t  <\/h3>\r\n\t\t\t\t\t 
        manufacturer:AMD
        modalDetail:A8-7670
        modal:AMD A8-7670
#-*- coding: UTF-8 -*-
import urllib
import re
from bs4 import BeautifulSoup
url='http://zj.zol.com.cn/index.php?c=Ajax_ParamResponse&a=GetGoods&subcateId=28&type=0&priceId=noPrice&page=2&manuId=&paramStr=&keyword=&locationId=1&queryType=0'
html = urllib.urlopen(url).read()
soup=BeautifulSoup(html,"html.parser")
listModal=[]
listSpecs=[]
tags = soup.find_all("a",attrs={"target":"\\\"_blank\\\""})
cnt=0
for tag in tags:
cnt+=1
modalSubstr=tag.contents[0]
#print 'modalSubstr:'+modalSubstr
manufacturer=re.findall('(.+?) ',modalSubstr)[0]#非贪心匹配 遇到空格即中止,返回第一个匹配项
#print 'manufacturer:'+manufacturer
detailSubstr=re.findall(' ([0-9a-zA-Z- ]+)',modalSubstr)
#print detailSubstr
detailSubstr0=detailSubstr[0]
#针对i3、i5、i7的处理
if "i3" in modalSubstr:
modalDetail="i3 "+detailSubstr0
elif "i5" in modalSubstr:
modalDetail="i5 "+detailSubstr0
elif "i7" in modalSubstr:
modalDetail="i7 "+detailSubstr0
else:
modalDetail=detailSubstr0
#针对APU的处理
if modalDetail=="APU":
modalDetail+=" "+detailSubstr[1] modal=manufacturer+" "+modalDetail
print "modal:"+modal
        效果

3.获取CPU主频

        except IndexError:因为中关村网站上最后一款CPU的主频信息暂无,所以针对这个情况它的规格(specs)为“Data Missed”
#-*- coding: UTF-8 -*-
import urllib
import re
from bs4 import BeautifulSoup
url='http://zj.zol.com.cn/index.php?c=Ajax_ParamResponse&a=GetGoods&subcateId=28&type=0&priceId=noPrice&page=2&manuId=&paramStr=&keyword=&locationId=1&queryType=0'
html = urllib.urlopen(url).read()
soup=BeautifulSoup(html,"html.parser")
listModal=[]
listSpecs=[]
tags = soup.find_all("a",attrs={"target":"\\\"_blank\\\""})
cnt=0
for tag in tags:
cnt+=1
print cnt
substr=str(tag)[100:500]
#以title='\"开头+任意小数+ GHz结尾
specsDictionary=re.findall(r'title=\'\\\"([0-9.]+GHz)',substr)
try:
specs=specsDictionary[0]
except IndexError:
specs="Data Missed"
print specs
        效果

4.循环读取下一页并自动终止

        一共有16页,本来可以直接用循环,但经观察发现每页开头的内容中有page值。而且当地址中的page>=16,index.php都只会返回page=16的内容。所以有了下面的代码用来循环读取下一页并自动终止。
        
 
urlLeft='http://zj.zol.com.cn/index.php?c=Ajax_ParamResponse&a=GetGoods&subcateId=28&type=0&priceId=noPrice&page='
urlRight='&manuId=&paramStr=&keyword=&locationId=1&queryType=0'
urlPageIndex=1
while (1):
url=urlLeft+str(urlPageIndex)+urlRight
html = urllib.urlopen(url).read()
soup=BeautifulSoup(html,"html.parser")
soupSub=str(soup)[0:50]
pageIndex=int(re.findall('page\":([0-9]+)',soupSub)[0])
if urlPageIndex==pageIndex:
tags = soup.find_all("a",attrs={"target":"\\\"_blank\\\""})
cnt=0
for tag in tags:
......省略
print "yes"+str(urlPageIndex)
urlPageIndex+=1
else:
print "no"+str(urlPageIndex)
break
 

5.输出为csv

        python内置了csv读取和导入,我参考crifan上的的csv导出
import csv
with open('excel_2010_ms-dos.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, dialect='excel')
for row in spamreader:
print ', '.join(row)

6.最终代码

#-*- coding: UTF-8 -*-
import urllib
import re
import csv
from bs4 import BeautifulSoup
listModal=[]
listSpecs=[]
urlLeft='http://zj.zol.com.cn/index.php?c=Ajax_ParamResponse&a=GetGoods&subcateId=28&type=0&priceId=noPrice&page='
urlRight='&manuId=&paramStr=&keyword=&locationId=1&queryType=0'
urlPageIndex=1
while (1):
url=urlLeft+str(urlPageIndex)+urlRight
html = urllib.urlopen(url).read()
soup=BeautifulSoup(html,"html.parser")
soupSub=str(soup)[0:50]
pageIndex=int(re.findall('page\":([0-9]+)',soupSub)[0])
if urlPageIndex==pageIndex:
tags = soup.find_all("a",attrs={"target":"\\\"_blank\\\""})
cnt=0
for tag in tags:
cnt+=1
modalSubstr=tag.contents[0]
manufacturer=re.findall('(.+?) ',modalSubstr)[0]#非贪心匹配 遇到空格即中止,返回第一个匹配项
detailSubstr=re.findall(' ([0-9a-zA-Z- ]+)',modalSubstr)
detailSubstr0=detailSubstr[0]
#针对i3、i5、i7的处理
if "i3" in modalSubstr:
modalDetail="i3 "+detailSubstr0
elif "i5" in modalSubstr:
modalDetail="i5 "+detailSubstr0
elif "i7" in modalSubstr:
modalDetail="i7 "+detailSubstr0
else:
modalDetail=detailSubstr0
#针对APU的处理
if modalDetail=="APU":
modalDetail+=" "+detailSubstr[1]
modal=manufacturer+" "+modalDetail
listModal.append(modal)
substr=str(tag)[100:500]
#以title='\"开头+任意小数+ GHz结尾
specsDictionary=re.findall(r'title=\'\\\"([0-9.]+GHz)',substr)
try:
specs=specsDictionary[0]
except IndexError:
specs="Data Missed"
listSpecs.append(specs)
print "yes"+str(urlPageIndex)
urlPageIndex+=1
else:
print "no"+str(urlPageIndex)
break
with open('Config.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, dialect='excel')
#write 标题行
spamwriter.writerow(['Config_Type','Config_Modal','Config_Specs','Config_MinorSpecs'])
i=0
for elementModal in listModal:
spamwriter.writerow(['CPU',listModal[i], listSpecs[i]])
i+=1

Python 爬取 中关村CPU名字和主频的更多相关文章

  1. 利用python爬取58同城简历数据

    利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...

  2. 利用Python爬取豆瓣电影

    目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...

  3. Python爬取LOL英雄皮肤

    Python爬取LOL英雄皮肤 Python 爬虫  一 实现分析 在官网上找到英雄皮肤的真实链接,查看多个后发现前缀相同,后面对应为英雄的ID和皮肤的ID,皮肤的ID从00开始顺序递增,而英雄ID跟 ...

  4. 萌新学习Python爬取B站弹幕+R语言分词demo说明

    代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...

  5. Python爬取网页信息

    Python爬取网页信息的步骤 以爬取英文名字网站(https://nameberry.com/)中每个名字的评论内容,包括英文名,用户名,评论的时间和评论的内容为例. 1.确认网址 在浏览器中输入初 ...

  6. steam夏日促销悄然开始,用Python爬取排行榜上的游戏打折信息

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 不知不觉,一年一度如火如荼的steam夏日促销悄然开始了.每年通过大大小小 ...

  7. Python爬取 | 唯美女生图片

    这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...

  8. Python爬取 | 王者荣耀英雄皮肤海报

    这里只展示代码,具体介绍请点击下方链接. Python爬取 | 王者荣耀英雄皮肤海报 import requests import re import os import time import wi ...

  9. Python 爬取途虎养车 全系车型 轮胎 保养 数据

    Python 爬取途虎养车 全系车型 轮胎 保养 数据 2021.7.27 更新 增加标题.发布时间参数 demo文末自行下载,需要完整数据私聊我 2021.2.19 更新 增加大保养数据 2020. ...

随机推荐

  1. 使用VS Code开发asp.net core (下)

    第一部分: https://www.cnblogs.com/cgzl/p/8450179.html 本文是基于Windows10的. Debugging javascript 打开wwwroot/js ...

  2. js跨域解决方案

    1.参考该文档:http://blog.csdn.net/enter89/article/details/51205752 2. 参考网络:http://www.ruanyifeng.com/blog ...

  3. 开发板访问linux方法

    1.使用网线分别将 PC 机与开发板连接到交换机. 2.保证 windows能 ping通 Linux. 2.1.关闭 windows 系统中的其他网络连接,只保留用来和交换机连接的网卡. 2.2.网 ...

  4. 阿里云ECS服务器上搭建keepalived+mha+mysql5.6+gtid+一主两从+脚本判断架构踩的坑

    最近,公司项目搭建了一套后端数据库架构,不是在RDS,是在阿里云的ECS服务器上搭建keepalived.mha.mysql5.6.gtid.一主两从架构,目前还没有实现读写分离,以后架构升级,可能代 ...

  5. Sqlserver将数据从一张表插入到另一张表

    1.如果是整个表复制表达如下: insert into table1 select * from table2 2.如果是有选择性的复制数据表达如下: insert into table1(colum ...

  6. lamp环境部署脚本

    关于lamp环境的安装脚本,直接复制即可使用 注:apache2.2.X 版本和apache2.4.X版本 本人推荐兼容性版本安装 apache2.4.25 + apr1.5.2 + apr-util ...

  7. ActiveRecord的生命周期

    ActiveRecord的生命周期,通过方法重写和插入我们需要的业务逻辑来达到我们对程序的控制. 示例: 1,beforeSave() public function beforeSave($inse ...

  8. 剑指offer随练

    合并两个排序的链表 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 思路:使用递归的方法,合并头节点,然后对剩下的链表接着合并头节点,直到合并完 ...

  9. 剑指offer 第一个只出现一次的字符 hash

    思路:i表示字符的ASCII码值,cntp[i]表示字符出现的次数. AC代码 class Solution { public: int FirstNotRepeatingChar(string st ...

  10. 历届试题 大臣的旅费 树形DP

    题目链接:大臣的旅费 思路:锦囊说用广搜,可惜这题没说数据范围,担心复杂度太高,我就直接用的树形DP--求树的最远路径. 以城市1为整棵树的根结点,d(i)表示以i为根结点的子树的最远路径,还有一个f ...