Python 爬取 中关村CPU名字和主频
0.准备工作
1.相关教程
2.说明
3.效果

1.获取CPU型号和主频信息
1.神伤的AJAX

2.获取CPU名字
#-*- 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=¶mStr=&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主频
#-*- 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=¶mStr=&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.循环读取下一页并自动终止

urlLeft='http://zj.zol.com.cn/index.php?c=Ajax_ParamResponse&a=GetGoods&subcateId=28&type=0&priceId=noPrice&page='
urlRight='&manuId=¶mStr=&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
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=¶mStr=&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名字和主频的更多相关文章
- 利用python爬取58同城简历数据
利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...
- 利用Python爬取豆瓣电影
目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...
- Python爬取LOL英雄皮肤
Python爬取LOL英雄皮肤 Python 爬虫 一 实现分析 在官网上找到英雄皮肤的真实链接,查看多个后发现前缀相同,后面对应为英雄的ID和皮肤的ID,皮肤的ID从00开始顺序递增,而英雄ID跟 ...
- 萌新学习Python爬取B站弹幕+R语言分词demo说明
代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...
- Python爬取网页信息
Python爬取网页信息的步骤 以爬取英文名字网站(https://nameberry.com/)中每个名字的评论内容,包括英文名,用户名,评论的时间和评论的内容为例. 1.确认网址 在浏览器中输入初 ...
- steam夏日促销悄然开始,用Python爬取排行榜上的游戏打折信息
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 不知不觉,一年一度如火如荼的steam夏日促销悄然开始了.每年通过大大小小 ...
- Python爬取 | 唯美女生图片
这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...
- Python爬取 | 王者荣耀英雄皮肤海报
这里只展示代码,具体介绍请点击下方链接. Python爬取 | 王者荣耀英雄皮肤海报 import requests import re import os import time import wi ...
- Python 爬取途虎养车 全系车型 轮胎 保养 数据
Python 爬取途虎养车 全系车型 轮胎 保养 数据 2021.7.27 更新 增加标题.发布时间参数 demo文末自行下载,需要完整数据私聊我 2021.2.19 更新 增加大保养数据 2020. ...
随机推荐
- (python基础)时间辍time、时间元组localtime、时间格式化strftime
可以直接将下方代码运行查看结果:#!/usr/bin/python# coding=utf-8import time # 引入time模块# 时间戳:# 每个时间戳都以自从1970年1月1日午夜(历元 ...
- Docker Compose容器编排
Compose是Docker官方的开源项目,可以实现对Docker容器集群的快速编排.Compose 中有两个重要的概念:服务(service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实 ...
- windows搭建web服务器
1.安装web管理工具 控制面板--->程序和功能--->打开或关闭功能 2.将internet信息服务中的所有选项全部勾上,并点击确定. 3.打开浏览器,输入http://localho ...
- Angular4---部署---Angular 与 Nginx的邂逅
Nginx + Angular结合操作 1.下载Nginx , 根据自己的版本下载Nginx,关于Nginx配置,请看https://www.cnblogs.com/MBirds/p/6605366. ...
- Windows实用快捷键
1 返回上一级目录:Backspace 2 复制当前窗口:Ctr + N 3 在当前文件目录中选定首个文件(夹):Space 4 切换窗口:Alt + Tab 5 当前浏览器Tab页倒退或前进历史页 ...
- LNMP环境下搭建wordpress
WordPress 下载WordPress安装包,可以直接wget获取也可以ftp上传,解压到/usr/share/nginx/html/blog-wp,访问index.php即进行安装: wor ...
- PHP将HTML的内容保存成word文档
<?php class word { function start() { ob_start(); echo '<html xmlns:o="urn:schemas-micros ...
- VC下防止反汇编的办法(1)
最近在看IDA的书,讲汇编语言的部分提到了一种防止递归向下汇编器逆向程序的方法 这里esp指向栈顶,也就是调用方最后入栈的返回地址.然而实际在VC2017里用内联汇编这么做是不行的,原因可以看看VC生 ...
- Android app性能测试小结(7个性能指标)
1.性能测试的几个指标: 2.性能测试环境准备: 3.启动时间 3.1,监控值的获取方法 启动分为冷启动和热启动,冷启动:应用程序首次启动,进程首次创建并加载资源的过程:热启动:应用程序启 ...
- UART知识总结
一.定义 通用异步收发传输器(Universal Asynchronous Receiver/Transmitter),通常称作UART. 二.时序 上图是uart协议传输一个"A" ...