beautifulsoup 简单使用

来源:互联网 发布:microservices java 编辑:程序博客网 时间:2024/05/29 09:09

直接上码,抓的是安智市场的数据

#-*-coding:utf-8-*-
from bs4 import BeautifulSoup
import urllib
import re
import xlwt
i = 1

data = xlwt.Workbook()
sheet = data.add_sheet(“sheet 1″)
row = 0
while i < 8:
path = ‘http://www.anzhi.com/list_1_’ + str(i) + ‘_hot.html’
html = urllib.urlopen(path)
url = “http://www.anzhi.com”
text = html.read()
soup = BeautifulSoup(text)
temps = soup.find_all(‘span’, attrs={“class”: “app_name”})
for temp in temps:
url_temp = url + temp.find(‘a’)['href']
if temp.find(‘a’)['href'] == ‘/soft_996309.html’:
continue
html = urllib.urlopen(url_temp)
text_app = BeautifulSoup(html)
app_temp = text_app.find(‘div’, {“class”:”detail_description”})
#游戏名称
app_name = app_temp.find(‘h3′)
app_name = app_name.text
sheet.write(row,0,app_name)
#下载次数

down_num = text_app.find(‘ul’,{“id”:”detail_line_ul”})
down_num = down_num.find(‘span’,attrs={“class”:”spaceleft”})
down_num = down_num.text
sheet.write(row,1,down_num)

developer_temp = text_app.find(‘div’,{“class”:”detail_description”})
dev_temps = developer_temp.find_all(‘div’,{“class”:”detail_line”})
dev_temp = dev_temps[2]
dev = dev_temp.find(‘span’)
temp = dev.text
sheet.write(row,2,temp)

#应用介绍
app_temp = text_app.find(‘div’,{“class”:”app_detail_list”})
app_info = app_temp.p.text
sheet.write(row,3,app_info)
#包名
app_wkey = text_app.find(‘div’,{“class”:”detail_icon”})
app_wkeys = app_wkey.find_all(‘a’)
for app_wkey in app_wkeys:
m = re.search(‘package=’,str(app_wkey))
if m is not None:
wkeys = re.split(‘package=’, str(app_wkey))
key = re.split(‘”>’,wkeys[1])[0]
sheet.write(row,4,key)
#下载地址
download = text_app.find(‘div’,{“class”:”detail_icon”})
down_temp = download.find(‘li’,{“id”:”install_wdj”})
down_temp = down_temp.find(‘a’)['href']
download_str = url + down_temp + “&n=5″
sheet.write(row,5, download_str)
row = row + 1
i = i + 1

data.save(‘test.xls’)

0 0
原创粉丝点击