snail:) 发表于 2018-3-9 21:10:07

每日一爬之爬取自定义车型和价位的汽车(好久没爬了哈哈哈~)

import requests
import bs4
import re
import openpyxl
def open_url(url):

    headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    html = requests.get(url,headers = headers)
    '''
    with open('car.txt','w',encoding = html.encoding)as f:
      f.write(html.text)
    '''
    return

def find_data(html):
    soup = bs4.BeautifulSoup(html.text,'html.parser')
    bs4_data = soup.find_all('p',class_='price')
    car_data_list = []
   
    for each in bs4_data:
      car_data_list.append(.strip()).group(1),re.search(r'(.+)万元',each.text.strip()).group(1)])
      
    return car_data_list
def save_as_excel(data,name):
    wb = openpyxl.Workbook()
    ws = wb.active

    ws['A1'] = '品牌'
    ws['B1'] = '车价'
    for each in data:
      ws.append(each)
    wb.save(name)
def main():
    car = {'2':'小型车','4':'中型车','6':'大型车'}
    url_data1 = input('请输入车型[小型:2,中型:4,大型:6]:')
    url_data2 = input('请输入价位(万元):')
    first_url = 'http://db.auto.sina.com.cn'

    url = first_url + '/list-' + url_data1 + '-0-' + url_data2 + '-0-0-0-0-0-9-0-1.html'
    html = open_url(url)
   
    name = car + url_data2 + '万元价位汇总.xlsx'
    data = find_data(html)
    save_as_excel(data,name)
   
if __name__ == '__main__':
    main()

焰虎 发表于 2018-9-1 15:34:35

大佬你好,你这个写入xlsx没有数据是什么问题
页: [1]
查看完整版本: 每日一爬之爬取自定义车型和价位的汽车(好久没爬了哈哈哈~)