马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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 [html,url]
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([re.search(r'(.+)报价',each.a['title'].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('请输入价位(万元)[n_m]:')
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)[0]
name = car[url_data1] + url_data2 + '万元价位汇总.xlsx'
data = find_data(html)
save_as_excel(data,name)
if __name__ == '__main__':
main()
|